php 日期时间到时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6225216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
datetime to timestamp
提问by medk
Possible Duplicates:
Inverse date function? - not strtotime
Is it possible to get UNIX time from such date 2011-02-27 02:04:46?
可能的重复项:
反向日期函数?- 不是 strtotime
是否可以从 2011-02-27 02:04:46 这样的日期获得 UNIX 时间?
hello,
你好,
we have this function to convert a timestamp to datetime:
我们有这个函数可以将时间戳转换为日期时间:
$datetime = date('Y-m-d H:i:s', $timestamp);
is there a function to do the opposite?
有相反的功能吗?
datetime to timestamp.
日期时间到时间戳。
Thanks
谢谢
回答by Aaria Carter-Weir
$timestamp = strtotime($datetime);
Or if you're confident of the format, split up the string with explode()
or even substr
and pass the necessary parts into the mktime
function.
或者,如果您对格式有信心,请使用explode()
或 even拆分字符串substr
并将必要的部分传递给mktime
函数。
Be aware that strtotime
can sometimes get the timestamp wrong, if a slightly unconventional format is used.
请注意strtotime
,如果使用稍微非常规的格式,有时可能会导致时间戳错误。
EDIT:
编辑:
A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat
eg:
这样做的一个真正准确的方法是,如果您知道您的输入格式,则使用DateTime::createFromFormat
例如:
$dateTimeObject = \DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');
See http://php.net/manual/en/function.date.phpfor formatting guides, and http://php.net/manual/en/datetime.createfromformat.phpfor info on the method described above.
见http://php.net/manual/en/function.date.php用于格式化指南和http://php.net/manual/en/datetime.createfromformat.php对于上面描述的方法的信息。
回答by Muhammad Zeeshan
$timestamp = strtotime('12-04-2010');