dongxuan8227 2017-03-06 11:39
浏览 52
已采纳

PHP约会2月问题

I'm using a code to find how many days have passed since the new year (January 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. I want it to count February as 28 or 29 days long, depending on the leap year condition. I guess there must be something in php.ini related to this. What and where to change or is there any hack?

  • 写回答

1条回答 默认 最新

  • douzen1896 2017-03-06 11:55
    关注

    To debug this issue with PHP incorrectly reporting difference between 2 dates we first must check a few things.

    I see a few people are getting mixed results, to debug this try find out what version of the timezonedb you have

    <?php
    echo timezone_version_get();
    ?>
    

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

    The PECL timezonedb site can be found at https://pecl.php.net/package/timezonedb

    Check to see that you have the most up to date version.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法