Jump to content

Date Format Conversion


lyndonw

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.