php - 为什么 strftime ('%G' , strtotime ('2017-01-01' )) 产生 2016 年?

标签 php strftime

我在 PHP 5.6 中发现了一个可能的错误。参数为 %G 的函数 strftime 生成 4 位数年份。然而,它似乎在喂食 1483246800 时返回错误的年份 - 即 2017 年 1 月 1 日。它返回 2016 年!

示例代码片段:

echo strftime('%G', strtotime('2017-01-01'));

这应该打印“2017”,但我得到的是“2016”。我正在运行 PHP 5.6。 这种极端情况也会出现在其他年份——例如2016-01-03 输出 2015 而不是 2016。

最佳答案

这不是错误。

作为someone pointed out在回答前一段时间其他人提交的错误报告时:

%G - The 4-digit year corresponding to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead...A reminder about ISO week numbers. They begin on mondays, end on sundays, and are considered a part of the year in which the majority of their days fall.

这意味着使用 %G 作为接近一年开始/结束的日期可以为您提供正确的年份,即您的情况下的 年,或者 年,(例如,echo strftime('%Y', strtotime('2002-12-30)) 给出 2003) .

如果你想获得正确的年份,你应该使用 %Y 代替。 echo strftime('%Y', strtotime('2017-01-01')); 给出 2017。

它也值得一试 the definition of %V :

ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week.

关于php - 为什么 strftime ('%G' , strtotime ('2017-01-01' )) 产生 2016 年?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41513143/

相关文章:

php - 在MySql中如果我不放置closeCursor会怎样?

php - 拉维尔 : Add data to the $request->all()

c - strftime 和 struct tm 中的两位数日期

php - 西类牙文字符显示不正确

php - Docker php : What are the :zts php images on Docker Hub

php - 在 CQRS 应用程序中强制执行验证/不变量

ubuntu - 如何使用 AWK 将时间戳转换为 UTC 时间

java - 使用 jQueryUI datepicker 和 php 获取一系列日期(西类牙语)

c - 如何通过api获取扩展的locale信息

php - jQuery 投票系统