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
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
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> ";
$WeekStart = date('jS F Y', $myrow["WeekStart"])."<br>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for that, worked fine. :)
Quite confusing that I have the mysql column as date but php reads it as string.
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?
ASKER
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.
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.
ASKER
Thanks for the explanation Mark and your help, really appreciate that.
Sounds like you need some sleep ;)
Sounds like you need some sleep ;)
For more info on PHP date format, this is a great reference.