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"?
Date reformatting.
Moderator: General Moderators
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Date reformatting.
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()
Now you are going to have to convert from format one into a timestamp and then pass that timestamp to date()
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
Re: Date reformatting.
ahh I have fallen in love with this function. Use the function strtotime(). Example just for you:
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.
Code: Select all
echo date('F jS Y g:ia', strtotime('2007-07-31 18:45:51'));
edit: hockey is right though, you should use timestamps in the future as they are easier to manage.
Re: Date reformatting.
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?
I understand now, timestamps are just integers representing the number of seconds from a standard date.
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.
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.How much harm does converting the date to timestamp using strtotime() do?