lyndonw Posted August 17, 2010 Share Posted August 17, 2010 I'm pulling some info from various XML files and am having problems converting the following date format so that it can be stored as a DATETIME field in a MySQL database. Tuesday,17 August 2010 09:25:00 This obvious newbie would appreciate a shove in the right direction... Thanks in advance Link to comment https://forums.phpfreaks.com/topic/210959-date-format-conversion/ Share on other sites More sharing options...
shlumph Posted August 17, 2010 Share Posted August 17, 2010 Use strtotime() to convert Tuesday,17 August 2010 09:25:00 to a timestamp: http://us3.php.net/manual/en/function.strtotime.php Have a look at the date function to convert the timestamp into the correct format: http://us3.php.net/manual/en/function.date.php Link to comment https://forums.phpfreaks.com/topic/210959-date-format-conversion/#findComment-1100328 Share on other sites More sharing options...
disciple10 Posted August 17, 2010 Share Posted August 17, 2010 This sounds like a job for DateTime::createFromFormat(). This function returns a timestamp returns a datetime value which date() can then use to generate the form that you want. Tuesday,17 August 2010 09:25:00 This date is in the format l,j F Y H:i:s where l (lowercase L) is the full name of the day of the week j is the day of the month without leading 0's. use "d" instead if it is "01" etc F is the full name of the month Y is the 4 digit year H is the 24-hour format number of hours with leading 0's i is the number of minutes with leading 0's s is the number of seconds with leading 0's (a full listing of format strings can be found here: http://php.net/manual/en/function.date.php) To use this information with DateTime::createFromFormat <?php $date = date_create_from_format('l,j F Y H:i:s', 'Tuesday,17 August 2010 09:25:00'); ?> Now you must construct the format string for the new datetime field. Mysql stores datetime info in the format YYYY-MM-DD HH:MM:SS. In PHP strings this would be "Y-m-d H:i:s" In order to get the datetime field you must plug the above into date() to get: <?php $datetime_value = date("Y-m-d H:i:s",$date); ?> more information on the date() function can be found at the link above. More info on the DateTime function can be found here: http://www.php.net/manual/en/datetime.createfromformat.php Link to comment https://forums.phpfreaks.com/topic/210959-date-format-conversion/#findComment-1100332 Share on other sites More sharing options...
lyndonw Posted August 17, 2010 Author Share Posted August 17, 2010 Thanks guys, I'll take a bash now... Link to comment https://forums.phpfreaks.com/topic/210959-date-format-conversion/#findComment-1100358 Share on other sites More sharing options...
lyndonw Posted August 17, 2010 Author Share Posted August 17, 2010 Thanks guys - I used a mixture of both your advice after a few errors (I presume because the data was an object not a string): // Lets convert the date into a usable format.. $date = strtotime((string) $data->StartTime); // And into a datetime friendly format $datetime_value = date("Y-m-d H:i:s", $date); Link to comment https://forums.phpfreaks.com/topic/210959-date-format-conversion/#findComment-1100378 Share on other sites More sharing options...
shlumph Posted August 17, 2010 Share Posted August 17, 2010 Cool beans Link to comment https://forums.phpfreaks.com/topic/210959-date-format-conversion/#findComment-1100395 Share on other sites More sharing options...
disciple10 Posted August 18, 2010 Share Posted August 18, 2010 Awesome Link to comment https://forums.phpfreaks.com/topic/210959-date-format-conversion/#findComment-1100598 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.