datetime到时间戳 [英] datetime to timestamp
问题描述
可能的重复:
反时限函数? - 不是strtotime
是否可以从这样的日期获取UNIX时间2011-02-27 02:04:46?
hello,
我们有这个函数将时间戳转换为datetime:
$ datetime = date('Ymd H:i:s',$ timestamp) ;
有没有办法做相反的功能?
datetime to timestamp。
谢谢
$ timestamp = strtotime($ datetime);
或者如果您对格式有信心,请使用 请注意,如果使用稍微非常规的格式, c有时可能会使时间戳错误。 一个非常准确的方法是如果你知道你的输入格式,就是使用 请参阅 http://php.net/manual/en/function.date.php 格式指南, http://php.net/manual/en/datetime.createfromformat.php 了解上述方法的信息。将
c $ c>甚至
substr
并将必要的部分传递到 mktime
函数中。 / p>
编辑:
DateTime :: createFromFormat
eg:
$ dateTimeObject = \DateTime :: createFromFormat(' G:我','9:30');
$ dateTimeObject-> format('H:i');
Possible Duplicates:
Inverse date function? - not strtotime
Is it possible to get UNIX time from such date 2011-02-27 02:04:46?
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
$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.
Be aware that strtotime
can sometimes get the timestamp wrong, if a slightly unconventional format is used.
EDIT:
A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat
eg:
$dateTimeObject = \DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');
See http://php.net/manual/en/function.date.php for formatting guides, and http://php.net/manual/en/datetime.createfromformat.php for info on the method described above.
这篇关于datetime到时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!