-1

I have two dates, which I need to know if the difference time does not exceed 72 hours between them. Including the seconds

Reading the Documentation of PHP I found that date_diff() was the solution for this, but it returns time in separate ways like this:

DateInterval {#1410 ▼
  interval: + 4d 10:42:00.0
  +"y": 0
  +"m": 0
  +"d": 4
  +"h": 10
  +"i": 42
  +"s": 0
  +"f": 0.0
  +"weekday": 0
  +"weekday_behavior": 0
  +"first_last_day_of": 0
  +"invert": 0
  +"days": 4
  +"special_type": 0
  +"special_amount": 0
  +"have_weekday_relative": 0
  +"have_special

So I use a format to get the time, but I also need a format to unite the hours and the seconds, this is my function

public function compare(){

 return $diff = (date_diff(new DateTime('2020-12-24T00:00:00'),new DateTime('2020-12-28T10:42:00')));
 $diff = $interval->format('%h')+(($interval->format('%d')*24)+($interval->format('%m')*28*24)+($interval->format('%y')*365*28*24))*3600;
}

  • Are you looking for a better way? – Gerard H. Pille Dec 28 '20 at 15:09
  • The second line in your function will never be executed since control returns on the first line. – Tangentially Perpendicular Dec 28 '20 at 15:09
  • If somebody answers your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – 0stone0 Dec 28 '20 at 21:46

3 Answers3

0

Using strtotime(), convert both the timestamps to UNIX format, then you can easily calculate the difference using seconds.

Afterwords, use a simple function to convert the seconds to HMS;

<?php


    $date_1 = strtotime('2020-12-24T00:00:00');
    $date_2 = strtotime('2020-12-28T10:42:00');
    
    // Get diff in seconds
    $diff = $date_2 - $date_1;
    
    // If $diff exeeds 72hours
    if ($diff > (72 * (60 * 60))) {
        echo "Exceed 72 hours" . PHP_EOL;
    }
    
    // Show diff + hms
    echo $diff . ' -- ' . toHMS($diff);
    
    
    function toHMS($seconds) {
      $t = round($seconds);
      return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
    }

Exceed 72 hours

384120 -- 106:42:00

Try the above PHP code online!

0stone0
  • 34,288
  • 4
  • 39
  • 64
0

You don't need to use date_diff there are functions on the DateTime classes themselves:


$date1 = new DateTime('2006-04-12T12:30:00');
$date2 = new DateTime('2006-04-14T11:30:00');

$diff = $date2->diff($date1);

$hours = $diff->h;


rattybag
  • 381
  • 2
  • 8
0

I managed to find the solution by using strtotime as follows

$date_1 = strtotime('2020-12-28 00:00:00');
    $date_2 = strtotime('2020-12-31 00:00:01');
    $diff = $date_2 - $date_1;
    // 259200 = 72 hours in seconds
    if ($diff > 259200) {
      echo 'exceeds 72 hours';
   }else
   {
    echo 'ended on time';
   }

By substracting the dates, I just need to compare the result with 259200 wich is the value of 72 hours in seconds, if its higher than that, then it exceeds the time limit.