Reputation: 165
I try to insert a rss pubdate to my MySQL database. The format of the date is:
Tue, 16 apr 2013 17:04:08 UT
I try different function to format this pubdate, but I have problems with the timezone UT in the date. Functions I tried:
strftime("%a, %d %b %G %H %M %S", strtotime($pubdate));
date_create_from_format('D, d M Y G:i:s U', $pubdate);
Can anybody help me?
Upvotes: 2
Views: 2039
Reputation:
You can do like this for inserting pubdate from RSS feed into mysql DateTime field
<?php
function pubDateToMySql($str) {
return date('Y-m-d H:i:s', strtotime($str));
}
$pubDate = 'Wed, 29 Jul 2015 06:59:41 +0000';
$mysql_datetime = pubDateToMySql($pubDate);
echo $mysql_datetime ;
Output
2015-07-28 23:59:41
ref : http://www.timwickstrom.com/server-side-code/php/convert-rss-pubdate-to-mysql-datetime/
Upvotes: 4
Reputation: 2021
you are trying to insert in to a mySQL DateTime field right? so your goal is to get it to be in this format (YYYY-MM-DD HH:MM:SS)
you can try this using strptime:
$rssdate = "Tue, 16 apr 2013 17:04:08 UT";
$parsed = strptime($rssdate,'%a, %d %b %Y %H:%M:%S %Z');
$sqldate = date("Y-m-d H:i:s", mktime($parsed['tm_hour'], $parsed['tm_min'], $parsed['tm_sec'], $parsed['tm_mon']+1, $parsed['tm_mday'], 1900+$parsed['tm_year']));
after that you should have gotten $sqldate with a value of
2013-04-16 17:04:08
take note that the UT
will be discarded because of the DateTime field not being able to handle it. If you really need it, then just change your row into a String type if you can.
Another thing, i tried using the date_create_from_format
too but it seems that it cant recognize the month of April as 'apr' since in the format it needs to be capitalized like 'Apr'.
Upvotes: 1