Format a MySQL date in PHP
I have a column in my table of type datetime
. I want to display them in a particular format, so I tried using this:
echo date( 'm/d/Y - g:h A', strtotime($dateFromMySQL) );
...which just messes everything up, completely.
If I simply do echo $dateFromMySQL;
(in a loop), I get this:
2011-09-10 01:03:04
2011-09-10 00:52:47 2011-09-10 00:40:26 and so on...
...which is correct.
If I do echo strtotime($dateFromMySQL);
, I get this:
1315630984
1315630367 1315629626 and so on...
...which might be correct. I wouldn't know.
However, when I do echo date( 'm/d/Y - g:h:s A', strtotime($dateFromMySQL) );
, I get this:
09/10/2011 - 1:01:04 AM
09/10/2011 - 12:12:47 AM 09/10/2011 - 12:12:26 AM and so on...
...which is obviously wrong.
So, my question is, what am I doing wrong?
Am I somehow not usingstrtotime
and date
corr开发者_StackOverflow中文版ectly?It should be m/d/Y - g:i:s A
I think you're looking for m/d/Y - g:i:s A
.
h
- hour
i
- minutes
You have used the wrong date format of h
correct behavior
date( 'm/d/Y - g:i:s A' ...
what is h & i
in PHP date function?
h = 12-hour format of an hour with leading zeros
i = Minutes with leading zeros
Yes. You ask for the hour in 12 h format and then for the hour in 24 h format.
Use 'g:i:s'
instead of 'g:h:s'
.
Additionally, I get a warning on trying:
PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Berlin' for 'CEST/2.0/DST' instead in Command line code on line 1
so this might be - or become - an additional issue.
精彩评论