Calculate number of days between two given dates

后端 未结 3 1482
忘了有多久
忘了有多久 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:24

    Here's an easy way:

    $depart = strtotime(implode(' ', $_POST['departon']));
    $return = strtotime(implode(' ', $_POST['returnon']));
    
    $diff = floor(($return - $depart) / (60 * 60 * 24));
    

    Note: there's only 30 days in June.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-25 17:38

    The mktime documentation specifies a number for the month, so you'd need to convert 'June' to '6'.

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