Im getting a result from a SQL query im running which outputs the following date ($mydate) (05/02/2011) .... It is formatted as dd/mm/yyyy ... When i run
date('l jS \of F Y',strtotime($mydate))
Im getting a completely different date to what is inputted, im gathering this is because im not using the mm/dd/yyyy form开发者_Go百科at.
How would i go about swapping it around for it to work properly?
Cheers.
You could use strptime()
to parse the date/time format. You could also convert the date string to YYYY-MM-DD which is always parsed correctly by strtotime. E.g:
$date = implode('-', array_reverse(explode('/', $date)));
But I agree with the other answers. Have a look at your database settings first. Virtually all databases are able to output YYYY-MM-DD format directly, and most will do so by default. See why yours doesn't.
I wonder why your date is coming out of your DB as dd/mm/yyyy, what kind of DB are you using? If MySQL, a DATE field should output YYYY-MM-DD which will behave correctly with strtotime().
You could explode('/', $mydate)
to manually split dd, mm and yyyy, and then reconstruct a new date string, or directly use the resulting numbers with mktime().
Here is a list of formats which strtotime()
understands.
Which SQL outputs such a weird date format? Or did you format the date yourself?
If it always comes in that format, you can use
$newtime = preg_replace("/\d\d\/\d\d/\d\d\d\d/", "$3/$2/$1", $date);
To convert it to yyyy/mm/dd, then just plug that into strtotime()
It's definitely not the most elegant solution, I would actually suggest reworking the database (create a new column, run a script to read each entry, generate the new date, then input that in, delete the old date field, then rename the new one, so this only has to be done once).
精彩评论