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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:42:25  来源:igfitidea点击:

datetime to timestamp

phpdatetimetimestamp

提问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 substrand pass the necessary parts into the mktimefunction.

或者,如果您对格式有信心,请使用explode()或 even拆分字符串substr并将必要的部分传递给mktime函数。

Be aware that strtotimecan 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::createFromFormateg:

这样做的一个真正准确的方法是,如果您知道您的输入格式,则使用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');