datetime format

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
playwright
Forum Newbie
Posts: 20
Joined: Wed Jun 02, 2010 6:11 pm

datetime format

Post by playwright »

Hello!! i want to save datetimes into a mysql database. Their format is dd-mm-yy hh:mm. I think mysql format for datetime is yy-mm-dd hh:mm..How can i convert them??
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: datetime format

Post by pickle »

Pull apart "their" format so you have a variable for the year, a variable for the month, etc. Then re-arrange them with string concatenation.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: datetime format

Post by jraede »

Try using strtotime, and then recreating the date in MySQL format using the php date() function. You may have to do str_replace to switch the hyphens with slashes, since sometimes strtotime doesn't recognize the datetime format you're using. But 9 times out of 10 it will give you the correct date from any reasonable format.
Post Reply