[英]Insert rss pubdate to MySQL database
I try to insert a rss pubdate to my MySQL database. 我尝试将rss pubdate插入我的MySQL数据库。 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. 我尝试使用不同的函数来格式化这个pubdate,但是我在日期中遇到了时区UT的问题。 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? 有谁能够帮助我?
You can do like this for inserting pubdate from RSS feed into mysql DateTime field 你可以这样做,将pubdate从RSS feed插入mysql DateTime字段
<?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/ 参考: http : //www.timwickstrom.com/server-side-code/php/convert-rss-pubdate-to-mysql-datetime/
you are trying to insert in to a mySQL DateTime field right? 你试图插入到mySQL DateTime字段对吗? so your goal is to get it to be in this format (YYYY-MM-DD HH:MM:SS) 所以你的目标是让它成为这种格式(YYYY-MM-DD HH:MM:SS)
you can try this using strptime : 你可以尝试使用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 之后你应该得到$ sqldate,其值为
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. 请注意,由于DateTime字段无法处理,因此将丢弃UT
。 If you really need it, then just change your row into a String type if you can. 如果你真的需要它,那么只要你可以将你的行改为String类型。
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'. 另一件事,我也尝试使用date_create_from_format
但它似乎无法将4月份视为'apr',因为它需要像'Apr'那样大写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.