【问题标题】:Why is the output of date format conversion and timestamp conversion different?为什么日期格式转换和时间戳转换的输出不同?
【发布时间】:2021-09-20 16:39:46
【问题描述】:

下面的时间戳和日期转换代码回显不同的输出。我还设置了默认时区,但不高兴。请问有人可以告诉我怎么了,因为我是第一次注意到这一点?我也在实时服务器上尝试过,结果是一样的。

echo date("d F, Y", 1578079200).'<br/>'; //03 January, 2020
echo strtotime("3 January, 2020").'<br/>'; //1609705200
echo date("d F, Y", 1609705200).'<br/>'; //03 January, 2021

非常感谢

【问题讨论】:

  • 如果去掉逗号,结果是什么? 2021 年是您计算机上的当前年份吗?

标签: php date time strtotime


【解决方案1】:
strtotime("3 January, 2020")

为您提供今年年 1 月 3 日的日期(剧透:20:20 小时,分钟)。

strtotime("3 January 2020")

为您提供 2020 年 1 月 3 日的日期(剧透:00:00 时,分)。


为什么日期格式转换和时间戳转换的输出不同?

因为那是不同的格式。每种不同格式的转换方式不同。

如果您确切了解日期格式并想解析日期,请参阅DateTime::parseFromFormat()

<?php
$date = DateTime::createFromFormat('d F, Y', '3 January, 2020');
echo $date->format('Y-m-d'), "\n";
echo $date->getTimestamp(), "\n";
2020-01-03
1578007000

【讨论】:

    猜你喜欢
    • 2016-11-26
    • 2021-09-17
    • 2019-02-25
    • 1970-01-01
    • 2016-11-14
    • 2019-07-23
    • 2015-08-19
    • 2020-02-23
    • 1970-01-01
    相关资源
    最近更新 更多