Calculate number of days between two given dates

后端 未结 3 1481
忘了有多久
忘了有多久 2021-01-25 16:42

Can anyone correct the error in my script to calculate the number of days between 2 dates. The dates have been input through a form, the variable info is as followed:

3条回答
  •  北海茫月
    2021-01-25 17:26

    The easiest way is by using datetimes.

    Consider this:

    var_dump(new DateTime('1 July 2007'));
    
    $a = new DateTime('1 July 2007');
    $b = new DateTime('1 June 2001');
    
    var_dump($a->diff($b));
    

    The var_dump will allow you to see the different kind of time you can extract from it.

    object(DateInterval)[3]
      public 'y' => int 6
      public 'm' => int 1
      public 'd' => int 0
      public 'h' => int 0
      public 'i' => int 0
      public 's' => int 0
      public 'invert' => int 1
      public 'days' => int 2221
    

    You can convert your array to a date time very easily by using

    $date = new DateTime(join(" ",$array));
    $date2 = new DateTime(join(" ",$array2));
    $diff = $date->diff($date2);
    

提交回复
热议问题