strtotime然后转换为UNIX?
编辑
根据下面的评论,这是我在实现 mktime 之前尝试拆分/组合/转换字符串...
$date1 = explode("T",'2005-03-27T00:00:00');
$date2 = explode("-", $date1[0]);
$try = mktime($time1,$time2,0,$date2[1],$date[2],$date[3]);
print $try;
// 打印输出:951793200
原始问题:
我已经继承了一个数据库,并且非常希望将数据存储的奇怪方式转换为对 mysql 更友好的方式...
与此同时,我得到一个 text 字符串(是的...文本)。 ..我愿意转换为 unixtime...
所以,我会得到一个如下所示的字符串:
2005-03-27T00:00:00 03:00 上午
我想将其转换为:
1111885200
日期和时间总是让我感到困惑...我已经使用 strtotime 和 mktime 做了很多事情,但无法按照我想要的方式对其进行格式化。
EDIT
Per the comment below, here's my attempt at splitting / combining / converting the string prior to implementing mktime...
$date1 = explode("T",'2005-03-27T00:00:00');
$date2 = explode("-", $date1[0]);
$try = mktime($time1,$time2,0,$date2[1],$date[2],$date[3]);
print $try;
// Prints out: 951793200
Original Question:
I've inherited a database and would like very much to convert the bizarre way the data is stored to something more mysql-friendly...
In the meantime, I get a text string (yes... text)... That I'd like to convert to unixtime...
So, I'll get a string that looks like this:
2005-03-27T00:00:00 03:00 AM
I'd like to convert it to:
1111885200
Dates and times always mess me up... I've done a number of things using strtotime and mktime, but can't get it formatted the way I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,我就是这样做的。
但是,根据您发布的日期,您想要的结果似乎不正确。
输出
Well, this is how I would do it.
But, your desired result does not seem to be correct based on the date you posted.
OUTPUT
您可以编写一个小型转换器脚本来为您完成该任务。
请检查 mktime 的可选参数
您可以尝试将字符串拆分为小时/分钟/秒/月/日/年并将其放入 mktime 中。然后 mktime 会给你相应的 unix 时间戳。
You could write a litte converter script that does the task for you.
Please check the optional parameters of mktime
You could try to split your string in hour / minute / second / month / day / year and put that into mktime. Then mktime will give you the unix timestamp for that.
因此,除非绝对必要,否则我不喜欢正则表达式,但在这里可能需要它们来挑选时区部分。
将为您提供看起来像这样的 $matches:
我会将这些片段放入 DateTime 对象。然后您可以将其输出为您想要的任何 格式。您可能还需要调整该正则表达式,以便它可以处理您的所有日期。
So, I'm not a fan of regular expressions unless absolutely necessary, but they might be necessary here to pick out the time zone piece.
Will give you $matches that look something like:
I would feed those pieces into a DateTime object. Then you can output it into any format you want. You also may have to adjust that regex some so it can handle all your dates.
如果 T00:00:00 部分没用,只需将其删除并使用
strtotime()
:这会打印:
If the T00:00:00 part is useless, just remove it and use
strtotime()
:This prints: