dongzhan5286 2012-11-21 09:33
浏览 106
已采纳

DateTime格式参考并确定天数到月份

I used DateTime to get the diff of two dates. Directly from the PHP documentation example:

$date1 = new DateTime('2012/03/15');
$date2 = new DateTime('2012/6/9');
$interval = $date1->diff($date2,true);
$days = $interval->format('%R%a days');

This will result in +86 days, I wonder where can I get the reference for those %R%a I don't know what they mean, but I just know by seeing that %R = + while %a is number of days.

Second, now by having the value 86 I can have at least a variable that I can use to tell that $date1 and $date2 is not within the length of 3 months (3 months is at least 90 days). I can simply use an if-else for this, however for precision, is there another way (built-in PHP functions or library) to determine that the value I have is within the period of 3 months?

  • 写回答

3条回答 默认 最新

  • dpw5865 2012-11-21 09:38
    关注
    1. Check the documentation for DateTime::diff.
    2. See that it returns a DateInterval, click link to its documentation.
    3. Read documentation for its format method.

    Use if ($interval->format('%m') > 3) to test if it's over three months. Notice that this is only the months portion of the interval, e.g. "3" of "2 years, 3 months". Take the years into account as well. You should not just use days for this, since there's no constant number of days in a month. 90 days and 3 months are not the same thing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)