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
PeterErhardAsked:
Who is Participating?
 
HellmarkConnect With a Mentor Linux Systems AdministratorCommented:
Assuming that $myrow["WeekStart"] contained a date like 2011-10-29, you'd need to convert that to a time first, as how you passed it would most likely be a string. Since it is getting a string, and not a time, you're confusing the date function.

Gotta have the strtotime function, like the examples I gave you in your other question.

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

Open in new window


0
 
HellmarkLinux Systems AdministratorCommented:
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.
0
 
PeterErhardAuthor Commented:
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>";

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
PeterErhardAuthor Commented:
Thanks for that, worked fine. :)

Quite confusing that I have the mysql column as date but php reads it as string.
0
 
HellmarkLinux Systems AdministratorCommented:
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?
0
 
PeterErhardAuthor Commented:
I didn't actually declare the myrow variable, just straight referred to it. Didn't realise I had to specifically declare it?

0
 
HellmarkLinux Systems AdministratorCommented:
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.
0
 
PeterErhardAuthor Commented:
Thanks for the explanation Mark and your help, really appreciate that.

Sounds like you need some sleep ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.