【问题标题】:parse UTC timestamp in PHP for only year, month, and date?仅在 PHP 中解析 UTC 时间戳为年、月和日期?
【发布时间】:2011-11-15 01:38:50
【问题描述】:

在 PHP 中获取 UTC 时间戳(整数)并仅将其解析为年、月和日的最佳方法是什么?谢谢

【问题讨论】:

  • UTC 时间戳是指“Unix 时间戳”吗?
  • 如果您指的是 unix 时间戳,您可以使用日期函数 (de.php.net/manual/en/function.date.php) 来收集详细信息。如果要解析格式化的时间戳,请举个例子。
  • 示例:strtotime("2011-9-12 05:48:00") 计算结果为 1315806480... 我希望能够将这个整数处理成 strtotime("2011- 9-12") 或 1315785600。
  • 和 NullUserException,不,我的意思是 UTC 时间戳。 en.wikipedia.org/wiki/Coordinated_Universal_Time

标签: php timestamp utc


【解决方案1】:
<?php
$time = strtotime(date('Y-m-d', *timestamp*));
?>

已编辑:

date() 函数获取您的时间戳并将其表示为人类可读的字符串,其中只有年、月和日期(即“2014-07-25”)并省略小时,分和秒。

然后 strtotime() 函数获取该人类可读的字符串并将其转换回您的时间戳,从而有效地将时间设为当天的 00:00。

【讨论】:

    【解决方案2】:

    DateTime 类非常适合这种任务

    <?php
    // for PHP5.2+ though
    var_dump(date_create('2011-9-12 05:48:00')->setTime(0, 0, 0)->format('U'));
    // if input is in integer already
    $input = 1315806480;
    var_dump(date_create("@{$input}")->setTime(0, 0, 0)->format('U'));
    

    【讨论】:

    • Britt 正在使用 Unix 时间戳,因此您可能希望 @1315806480 代替 2011-9-12…
    • 大声笑,我认为输入会类似于 ISO 日期时间格式
    • erm,我实际上已经阅读了 cmets,因为她提到了 '2011-9-12 05:48:00',所以我最初的回答只是认为大声笑
    【解决方案3】:

    使用 PHP date() 函数。

    <?php
    $timestamp = strtotime("2011-9-12 05:48:00");
    $year = date('Y', $timestamp);
    $month = date('m', $timestamp);
    $day = date('d', $timestamp);
    $newTimestamp = mktime(0, 0, 0, $month, $day, $year);
    

    【讨论】:

    • 不是我真正想要的.. 它不会返回 unix 时间戳方面的值.. 而且,我不需要将它们分开.. 请参阅我在评论中给出的示例到我的帖子。谢谢
    • 哦哇哦..我什至没有阅读该代码的最后一行..对..我想这就是这样做的方法..谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2016-05-22
    • 2023-03-17
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多