Welcome to WebmasterWorld Guest from 50.19.190.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php date format conversion

Converting a date submitted through a form

     
11:59 pm on Mar 18, 2013 (gmt 0)

Junior Member

joined:June 6, 2012
posts: 104
votes: 0


I have a form input that accepts a date and inserts it into a MySQL database. The current format is YYYY, MM-DD. This is the format that I require on one of the forms that allows the user to input a date range for a custom report. I'm going to replace the manual input with a graphical calendar using jquery but it's been requested that I change the date formate to MM/DD/YYYY.

In a nut shell, how can I convert MM/DD/YYYY to YYYY, MM-DD so the database will recognize it?

Thanks
-Brandon
12:41 am on Mar 19, 2013 (gmt 0)

New User

joined:Mar 7, 2013
posts: 30
votes: 0


Maybe this will help

$date = '01/24/2006';
echo date('Y-m-d', strtotime($date)); // outputs 2006-01-24
12:54 am on Mar 19, 2013 (gmt 0)

Junior Member

joined:June 6, 2012
posts: 104
votes: 0


Thanks Skorpios,

I have to pass the date variable back to an sql statement, so could I do this:

$date = '01/24/200';
$new_date = date('Y, m-d', strtotime($date));

And then use $new_date in my sql statement instead of $date?

Thanks
12:03 pm on Mar 19, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


Looks about right, did you try it? Also, why not just use the ISO date format as your database table column definition? It would be an easy adjustment right away and you can then use all kinds of date features during SELECT queries. It will make your life much easier I would think!
12:13 pm on Mar 19, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


MM/DD/YYYY is ambiguous for non-US readers for the first 12 days of every month.

YYYY-MM-DD is never ambiguous (yyyy-dd-mm is not used anywhere).

See also RFC 3339.