Page 1 of 1
Date reformatting.
Posted: Sat Jul 19, 2008 6:54 pm
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"?
Re: Date reformatting.
Posted: Sat Jul 19, 2008 7:14 pm
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()
Re: Date reformatting.
Posted: Sat Jul 19, 2008 7:22 pm
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.
Re: Date reformatting.
Posted: Sun Jul 20, 2008 12:13 am
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.

(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?
Re: Date reformatting.
Posted: Sun Jul 20, 2008 4:30 pm
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.