Date reformatting.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Date reformatting.

Post by JellyFish »

Hey, is there a way I could reformat a date string from a mySQL field?

For example, let's say I get the string "2007-07-31 18:45:51" from the my database, how could I reformat this into "July 31st 2007 6:45pm"?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Date reformatting.

Post by alex.barylski »

This is why I use timestamps and not the date field type...

Now you are going to have to convert from format one into a timestamp and then pass that timestamp to date()
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Date reformatting.

Post by shiznatix »

ahh I have fallen in love with this function. Use the function strtotime(). Example just for you:

Code: Select all

 
echo date('F jS Y g:ia', strtotime('2007-07-31 18:45:51'));
 
Check out date() and strtotime() in the manual for further assistance.

edit: hockey is right though, you should use timestamps in the future as they are easier to manage.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Date reformatting.

Post by JellyFish »

Thank you guys very much! I so much appreciate it.

I understand now, timestamps are just integers representing the number of seconds from a standard date. :D (was trying to figure out what it was)

Hmm, so you both are saying to use the timestamp data type rather then date for my sql table? How much harm does converting the date to timestamp using strtotime() do?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Date reformatting.

Post by alex.barylski »

How much harm does converting the date to timestamp using strtotime() do?
Harm? In terms of performance if you converted it everytime you pulled on records, probably lots. If you write a quick and dirty script to convert the dates into timestamps no harm at all as long as the conversion goes as expected.
Post Reply