【问题标题】:php get date from epoch msphp 从 epoch ms 获取日期
【发布时间】:2012-10-06 14:25:45
【问题描述】:

我有一个从纪元开始以毫秒为单位的字段,例如 1350393140000。我需要在 PhP 中将此字段作为日期时间,将其时间部分更改为上午 7 点(UTC 时间)并将其从纪元转换回毫秒。我该怎么做?

【问题讨论】:

  • 如果您知道时间戳的时区,您可以轻松地减去/添加与 UTC 的差异。
  • @Veseliq:unix 时间戳始终采用 UTC。但是你怎么知道区别呢?

标签: php epoch


【解决方案1】:
$input = 1350393140000;

$dt = new DateTime('@' . floor($input / 1000));
$dt->setTimeZone(new DateTimeZone('UTC'));
$dt->modify('07:00');

$output = $dt->format('U') * 1000;

【讨论】:

  • 哎呀,忽略我,对上一个问题感到困惑!
  • 完美!感谢您的快速回复!
【解决方案2】:
date_default_timezone_set('UTC');
$ts = 1350393140000 / 1000;
$ts = mktime(7, date('i', $ts), date('s', $ts), date('n', $ts), date('j', $ts), date('Y', $ts));
$ts = $ts * 1000;

【讨论】:

  • 但事实上,你现在应该使用 DateTime 类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-11
  • 2017-06-18
  • 2013-03-16
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 2015-10-21
相关资源
最近更新 更多