php:将毫秒转换为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/557959/
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-24 23:04:37  来源:igfitidea点击:

php: convert milliseconds to date

phpdatetimetime

提问by Jeff Winkworth

I Have a string that is equal to a date, represented as the number of milliseconds since the Unix epoch.

我有一个等于日期的字符串,表示为自 Unix 纪元以来的毫秒数。

I am trying to output it into d-m-Y.

我正在尝试将其输出到 dmY。

The string I was given was "1227643821310", and I am told that the result should be equal to 2-12-2008, but I keep getting a result of 25-11-2008

我得到的字符串是“1227643821310”,我被告知结果应该等于2-12-2008,但我一直得到25-11-2008的结果

My code is as follows:

我的代码如下:

$mil = 1227643821310;
$seconds = $mil / 1000;
echo date("d-m-Y", $seconds);

Any ideas as to why this might be?

关于为什么会这样的任何想法?

采纳答案by Patrick Glandien

You are already doing it right, 1227643821 is simply not 02-12-2008, it is indeed 25-11-2008.

您已经做得对了,1227643821 根本不是 02-12-2008,它确实是 25-11-2008。

回答by DanielOpaluwa

I just added H:i:s like in the below example:

我刚刚在下面的示例中添加了 H:i:s:

$mil = 1227643821310;
$seconds = $mil / 1000;
echo date("d/m/Y H:i:s", $seconds);

回答by Scott

The only thing I can think of is try rounding off the decimal portion before converting it to a date. If that doesn't change the result, then the result is correct.

我唯一能想到的是在将小数部分转换为日期之前尝试四舍五入。如果这不会改变结果,那么结果就是正确的。

回答by Ilya Birman

Jeff, the important thing to understand when dealing with timestamps: they represent time which have passed from 0:00:00 01.01.1970 in GMT, not in your timezone (unless you are youself in GMT, of course).

杰夫,在处理时间戳时要了解的重要一点:它们代表从 0:00:00 01.01.1970 开始的时间在 GMT 中,而不是在您的时区中(当然,除非您自己在 GMT 中)。

1227643821 indeed represents the GMT time of 20:10:21 25.11.2008.

1227643821 确实表示 2008 年 11 月 25 日 20:10:21 的格林威治标准时间。

This is November 25th, 2008 in most of the world, however in timezones to the east of Moscow (and in the Moscow timezone itself in summer because of daylight savings time) it's already November 26th. Since the most “extreme” east time zone is GMT+14, there's no place in the world where the timestamp of 1227643821 can represent a date later then the 26th.

在世界大部分地区,这是 2008 年 11 月 25 日,但在莫斯科以东的时区(由于夏令时,莫斯科时区本身在夏季)已经是 11 月 26 日。由于最“极端”的东部时区是 GMT+14,因此世界上没有任何地方可以用 1227643821 的时间戳表示 26 日之后的日期。

Author of the original value may have somehow mistaken when dealing with timezones. Or just plain mistaken. For example, when calculating the value, added seconds instead of milliseconds at some step.

原始值的作者在处理时区时可能有某种错误。或者只是完全错误。例如,在计算值时,在某些步骤添加秒而不是毫秒。

回答by atlas

For the conversion itself, I use this line: $date = date('d-m-Y H:i:s', $millis / 1000);

对于转换本身,我使用这一行: $date = date('d-m-Y H:i:s', $millis / 1000);

Although the answer is simple, I like to post an example snippet for usage as well, so there it is.

虽然答案很简单,但我也喜欢发布一个示例代码片段以供使用,所以就这样了。

Extracting day, monthand yearfrom it.

从中提取

// explode values first in spaces and then in dashes
$date = explode('-', explode(' ', $date)[0]); 
$day = $date[0];
$month = $date[1];
$year = $date[2];

Use them as you like: echo $day . '-' . $month . '-' . $year;

随意使用它们: echo $day . '-' . $month . '-' . $year;

Output: dd-mm-yyyy

输出: dd-mm-yyyy

回答by Adonias Vasquez

$mil = 1227643821310;
$seconds = ceil($mil / 1000);
echo date("d-m-Y", $seconds);