Link to home
Start Free TrialLog in
Avatar of PeterErhard
PeterErhard

asked on

Format Date

2011-10-29      

I'm returning dates like the above from the database. How can I display them in a nice format like 29th October 2011.

I tried this:

$WeekStart = date('D, d M Y', $myrow["WeekStart"]);

but I get this with the above:

Wed, 31 Dec 1969
Avatar of Keith Brown
Keith Brown
Flag of United States of America image

D is the day of the week, d is the numeral date for the day, M is for the 3 character length form of the month, Y is for the 4 digit year. For getting a result like "29th October 2011", you need to have the format set to "jS F Y". j gives the numeral date for the day, S is for the suffix, F gives the full name of the month.

For more info on PHP date format, this is a great reference.
Avatar of PeterErhard
PeterErhard

ASKER

Thanks for that, but how come I would get returned "31st December 1969" from "2011-10-29" with the following code:

$WeekStart = date('jS F Y', $myrow["WeekStart"])."<br>";

ASKER CERTIFIED SOLUTION
Avatar of Keith Brown
Keith Brown
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for that, worked fine. :)

Quite confusing that I have the mysql column as date but php reads it as string.
Just because mysql stores it as a date, doesn't mean that PHP places it in a variable as a date. How did you declare the myrow variable?
I didn't actually declare the myrow variable, just straight referred to it. Didn't realise I had to specifically declare it?

Sorry, long day, little sleep, and have been dealing with some C++ code.

PHP doesn't do variable type declaration like most other languages. It instead sets the variable to the type of what ever it was given, which can cause hiccups in some cases,  like this. PHP only has variable type support for integers, boolean, floats, strings, arrays, and objects. Since it doesn't have a specific date or time variable, it just treated the info from mysql as a string, and when you passed it along, since it wasn't in a unix time format, thats where the problem came in. It was expecting it in one format, and wasn't smart enough to figure it was in another, because there is no clues from the variable itself.
Thanks for the explanation Mark and your help, really appreciate that.

Sounds like you need some sleep ;)