PHP: What are these Date (diff?) format parameters (eg. %R%a)

后端 未结 2 1672
醉酒成梦
醉酒成梦 2021-01-20 03:35

I runned into few examples where people share code for calculating the difference between two days.

Eg.

$now = new DateTime();
$itemDate->diff($n         


        
相关标签:
2条回答
  • 2021-01-20 04:13

    r - Sign "-" when the difference is negative, empty when positive a - Total number of days as a result of a DateTime::diff() or (unknown) otherwise.

    As an example,

    <?php
    $now = new DateTime();
    $d = new DateTime('2019-01-01T15:03:01.012345Z');
    $x = $d->diff($now)->format("%r%a");
    
    echo $x;
    
    ?>
    

    Output: string(4) "-287"

    0 讨论(0)
  • 2021-01-20 04:16

    When doing difference between DateTimeInterface objects, DateInterval object will be returned. You don't have DateTime's anymore, you have interval, and intervals are formatted different as DateTime objects. Format is explained here: http://php.net/manual/en/dateinterval.format.php

    0 讨论(0)
提交回复
热议问题