Einius
Einius

Reputation: 1382

PHP Date time conversion to save to MySQL

I need to get data from RSS feed and then save it to MySQL. The problem is that in RSS feed datetime format is like this: Sun, 09 Nov 2014 12:00:38 +0200 How I could convert it to format so I could save it to database? and how to convert it back later when I want to display it again with the same format?

Upvotes: 0

Views: 343

Answers (2)

Frankey
Frankey

Reputation: 3755

if you are using PHP along with MySQL, strtotime() is nice php function :)

http://php.net/manual/en/function.strtotime.php

date_default_timezone_set('UTC');

$date_string = 'Sun, 09 Nov 2014 12:00:38 +0200';
echo 'original string: '.$date_string.'<br/>'; 

$unix_time_stamp = strtotime($date_string );  
echo 'timestamp: '.$unix_time_stamp.'<br/>';    

$old_format = date("D, j M Y H:i:s O", $unix_time_stamp );  
echo 'back to originalt: '.$old_format; 

Example on http://viper-7.com/KI5LfG

You can get any date format you desire with php date()

http://php.net/manual/en/function.date.php

Upvotes: 0

Indra Kumar S
Indra Kumar S

Reputation: 2945

Try this

        $DateTime= date("Y-m-d H:i:s", strtotime("Sun, 09 Nov 2014 12:00:38 +0200"));
         echo  $DateTime;

To retrieve back from db, in your select query use

      DATE_FORMAT(date_column, '%a %d %b %Y %T')

Upvotes: 2

Related Questions