php 将人性化的日期转换为毫秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1532534/
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
Converting human-friendly date to milliseconds
提问by stunnaman
How to convert human-friendly date to milliseconds since the unix epoch?
自 Unix 纪元以来,如何将人类友好的日期转换为毫秒?
回答by chaos
strtotime($human_readable_date) * 1000
回答by drAlberT
Pay attention: strtotime() * 1000 is ok to have seconds expressed as milliseconds!
注意: strtotime() * 1000 可以将秒表示为毫秒!
The right answer is that it is not possible to have a millisecond precision on date/time functions in PHP. The precision of Unix Epoc based functions is only of 1k milliseconds, aka second :)
正确的答案是 PHP 中的日期/时间函数不可能有毫秒精度。基于 Unix Epoc 的函数的精度仅为 1k 毫秒,也就是秒 :)
Using the suggested answers you don't have milliseconds, but seconds expressed as number of milliseconds.
使用建议的答案,您没有毫秒,而是以毫秒数表示的秒数。
If you are aware of this, and you don't really need a millisecond precision then the answers given are ok, but the question was wrong :)
如果您知道这一点,并且您真的不需要毫秒精度,那么给出的答案是可以的,但问题是错误的:)
回答by Dominic Rodger
You're looking for strtotime.
您正在寻找strtotime.
Sample Usage:
示例用法:
$myvar = strtotime("7 October 2009");
That gives you seconds since the Unix epoch, so you want:
自 Unix 时代以来,这为您提供了几秒钟的时间,因此您需要:
$myvar = strtotime("7 October 2009") * 1000;
Watch out for the fact that strtotime"guesses" what you mean (how should it interpret "12-08-2009"? probably as 8th December, but it might equally validly - and being a Brit, thoroughly sensibly - guess 12th August). If you know the format in advance, use strptime.
注意strtotime“猜测”你的意思的事实(它应该如何解释“12-08-2009”?可能是 12 月 8 日,但它可能同样有效 - 作为一个英国人,完全明智地 - 猜测 8 月 12 日)。如果您事先知道格式,请使用strptime.

