Jump to content


Photo

Date Conversion


  • Please log in to reply
4 replies to this topic

#1 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 27 June 2006 - 08:45 PM

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?

#2 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 27 June 2006 - 08:51 PM

easy:

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

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

#3 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 27 June 2006 - 09:17 PM

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

#4 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 28 June 2006 - 06:10 AM

I used the code exactly as you put it, now all my dates are showing like this:

December 31, 1969

What happened?

#5 adamwhiles

adamwhiles
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 28 June 2006 - 06:30 AM

Finally figured it out, here is the finally working code for future reference:

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users