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.

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

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真