从电子邮件标题转换数据 [英] Convert data from email header
问题描述
有人可以帮助我如何转换电子邮件标题中的数据吗?
Does anyone could help me how to convert data from email header?
我有电子邮件标题中的下一个日期格式:
星期三,2010年4月28日21:59:49 -0400
I have the next date format from email header:
Wed, 28 Apr 2010 21:59:49 -0400
我需要将它们转换为mysql日期或时间戳。谢谢!
I need to convert them into mysql Date, or timestamp. Thanks!
推荐答案
您应该使用 DateTime
为此,特别是 DateTime :: createFromFormat()
:
You should be using DateTime
for this, specifically DateTime::createFromFormat()
:
$str = 'Wed, 28 Apr 2010 21:59:49 -0400';
$date = DateTime::createFromFormat( 'D, d M Y H:i:s O', $str);
现在,您在 $ date $ c $中有一个Date对象。 c>,然后可以获取unix时间戳(如果需要的话),也可以将其格式化为MySQL的日期。
Now, you have a Date object in $date
, and you can grab the unix timestamp (if that's what you want), or you can format it into a date for MySQL.
echo $date->getTimestamp(); // Outputs: 1272506389
echo $date->format( 'Y-m-d H:i:s'); // For MySQL column, 2010-04-28 21:59:49
您可以看到它正常运行在演示中。
You can see it working in the demo.
这篇关于从电子邮件标题转换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!