Jump to content

Archived

This topic is now archived and is closed to further replies.

adamwhiles

Date Conversion

Recommended Posts

I have a field in my database that holds a date like so: 2006-06-23

I want to convert that with php so I can print it out like this: June 23, 2006

I need to keep the format of the date in the table because it needs to be 2006-06-23 due to another script that uses the date in that format.

So how can i use PHP to pull the date out of the database and convert it to the longer format?

Share this post


Link to post
Share on other sites
easy:

[code]
$oldDate = '2006-06-23';
$answer = date('F d, Y',strtotime(date('Y-m-d',$oldDate)));
[/code]

so ur taking the old date, converting it to a unix timestamp, then reformatting it to how you want to see it.

FYI - it's easier just to start out with the Unix timestamp in the DB. =D

Share this post


Link to post
Share on other sites
I think I may do that, change the field to a unix time stamp, is there a certain type of field type it needs to be in mysql? Right now the field type is DATE, i know DATE won't work for unix time stamp, so what should it be?

Thanks

Share this post


Link to post
Share on other sites
I used the code exactly as you put it, now all my dates are showing like this:

December 31, 1969

What happened?

Share this post


Link to post
Share on other sites
Finally figured it out, here is the finally working code for future reference:

[code]
$oldDate = '2006-06-23';
$answer = date('F d, Y',strtotime($oldDate));
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.