Hi guys, I have a date field in MySql database table to enter dates. In my php code I used long date style to enter date value. So the data in Date field of the table is in following format. 08th of September 2011 But now I want to change that date to short date format which should looks like this.... 08-09-2011 To convert this I tried following code, but didn't work. $todayDate = "08th of September 2011"; $new_date = date('d-m-Y', strtotime($todayDate)); Can somebody help me to convert this long date to short date style in PHP please?
that strtotime should work but if its wasting you too much time and the format is consistent then explode it and parse it yourself. The database should be saving it as Y-m-d (big date measure, middle date measure, smallest date measure) which also makes sorting feasible. Personally I'd only recommend that verbose date formatting for presentation of data, never for input. If people are setting a date on a form they can always select September from a dropdown but the saved value would be 09, etc
Why not just link a calendar (jQuery has a nice one) to the input box so they can pick the date? That way you'll get a date formatted in way that it can be inserted into a database.
The "of" screws up strtotime. 08th of September 2011 08th September 2011 should work fine, though adds an annoyance in removing the word. I don't know if it's possible for you, but what I would advise is storing the date in the the timestamp format, instead of using the DATE type in MySQL. For example: <?php //Before data is added to the database. $time = '08th September 2011'; $timeStamp = strtotime($time); #Store $timeStamp in your database field. #In this case, $timeStamp = 1315404000 #When retrieving the date from your #database, do this: $time = '1315404000'; //This has been retrieved from the date field. $formattedTime = date('d-m-Y', $time); ?> PHP: Hopefully this helps. Cheers