简体   繁体   English

PHP Datediff 2月问题

[英]PHP datediff february issues

I'm using a code to find how many days have passed since the new year (January 1). 我正在使用代码来查找自新年(1月1日)以来已经过了多少天。 I use this code: 我使用以下代码:

$newyear = new DateTime("2017-01-01"); // January 1, 2017.
$today = new DateTime(); // March 6, 2017.
$difference = $newyear->diff($today);

Now, if I use this code, it shows the right number of days, 现在,如果我使用此代码,它将显示正确的天数,

echo $difference->format("%a days"); // 64 days

but when I use a different format, it goes wrong. 但是当我使用其他格式时,会出错。

echo $difference->format("%m months %d days"); // 2months 2 days.

I assume this is due to PHP counting February as 31 days long, so technically it is 31 + 31 + 2 days. 我认为这是由于PHP计算的2月为31天,因此从技术上讲,它是31 + 31 + 2天。 I want it to count February as 28 or 29 days long, depending on the leap year condition. 我希望根据February年的情况,将2月定为28或29天。 I guess there must be something in php.ini related to this. 我猜php.ini中一定与此有关。 What and where to change or is there any hack? 什么以及在哪里进行更改或有任何破解?

To debug this issue with PHP incorrectly reporting difference between 2 dates we first must check a few things. 要用PHP错误地报告两个日期之间的差异来调试此问题,我们首先必须检查一些事情。

I see a few people are getting mixed results, to debug this try find out what version of the timezonedb you have 我看到几个人的结果参差不齐,要进行调试,请尝试找出您所使用的timezonedb版本

<?php
echo timezone_version_get();
?>

http://php.net/manual/en/function.timezone-version-get.php http://php.net/manual/en/function.timezone-version-get.php

The PECL timezonedb site can be found at https://pecl.php.net/package/timezonedb PECL timezonedb网站可以在https://pecl.php.net/package/timezonedb中找到

Check to see that you have the most up to date version. 检查以确保您拥有最新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM