?
Solved

Format Date

Posted on 2011-10-29
8
Medium Priority
?
252 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:PeterErhard
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:Hellmark
ID: 37051982
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
 

Author Comment

by:PeterErhard
ID: 37052089
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
 
LVL 7

Accepted Solution

by:
Hellmark earned 2000 total points
ID: 37052099
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PeterErhard
ID: 37052106
Thanks for that, worked fine. :)

Quite confusing that I have the mysql column as date but php reads it as string.
0
 
LVL 7

Expert Comment

by:Hellmark
ID: 37052135
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
 

Author Comment

by:PeterErhard
ID: 37052406
I didn't actually declare the myrow variable, just straight referred to it. Didn't realise I had to specifically declare it?

0
 
LVL 7

Expert Comment

by:Hellmark
ID: 37052460
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
 

Author Comment

by:PeterErhard
ID: 37052464
Thanks for the explanation Mark and your help, really appreciate that.

Sounds like you need some sleep ;)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question