Asked  1 Year ago    Answers:  5   Viewed   10 times

I'm trying to get the difference between two datetimes and return it as a datetime. I've found examples using diff but I can't seem to get it right.

$timein = date("Y-m-d H:i:s");
$timeout = date("Y-m-d 20:00:00");
$totaltime = $timein->diff($timeout);

However $totaltime logs 0000-00-00 00:00:00 to my DB. Is this because I'm not formatting my totaltime variable?

 Answers

4

I'm not sure what format you're looking for in your difference but here's how to do it using DateTime

$datetime1 = new DateTime();
$datetime2 = new DateTime('2011-01-03 17:13:00');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $elapsed;
Friday, June 4, 2021
 
Jesse
 
4

Hmm fast solution, not optimized

$result = array();
foreach($a1 as $va1) {
   $found = false;
   foreach($a2 as $va2) {
      $x = array_diff($va1, $va2);
      if (empty($x)) {
         $found = true;
      }
   }
   if (!$found) { 
      $result[] = $va1;
   }
}

foreach($a2 as $va2) {
   $found = false;
   foreach($a1 as $va1) {
      $x = array_diff($va2, $va1);
      if (empty($x)) {
         $found = true;
      }
   }
   if (!$found) { 
      $result[] = $va2;
   }
}

var_dump($result);

EDIT: A little optimized (unseting values that were found before):

$a1= array(
      array('a'=>1,'b'=>2, 'c'=>3), // similar to $a2[0]
      array('a'=>3,'b'=>4, 'c'=>5), // similar to $a2[1]
      array('a'=>9,'b'=>6, 'c'=>9)
);
$a2= array(
      array('a'=>1,'b'=>2, 'c'=>3),
      array('a'=>3,'b'=>4, 'c'=>5),
      array('a'=>5,'b'=>6, 'c'=>7),
      array('a'=>11,'b'=>4, 'c'=>13),
      array('a'=>14,'b'=>6, 'c'=>3)
);
$i=0;
$result = array();
    foreach($a1 as $ka1 => $va1) {
       $found = false;
       foreach($a2 as $ka2 => $va2) {
          $i++;
          $x = array_diff($va1, $va2);
          if (empty($x)) {
             $found = true;
             unset($a2[$ka2], $a1[$ka1]);
          }
       }
       if (!$found) { 
          $result[] = $va1;
       }
    }
    foreach($a2 as $ka2 => $va2) {
   $found = false;
   foreach($a1 as $ka1 => $va1) {
      $i++;
      $x = array_diff($va2, $va1);
      if (empty($x)) {
         unset($a2[$ka2], $a1[$ka1]);
         $found = true;
      }
   }
   if (!$found) { 
      $result[] = $va2;
   }
}

var_dump($result);echo $i;
Thursday, April 1, 2021
 
3

You need to call DateInterval::format() to display that difference as a string.

echo $diff->format('%d days');

See the manual for all of the available formatting options.

Wednesday, July 7, 2021
 
mattltm
 
2
<?php
$date = new DateTimeImmutable("2018-07-31");
$previous = $date->sub(new DateInterval('P1M'));
$lastMonth= $date->modify('last day of previous month');
if ($previous > $lastMonth) {
    $previous = $lastMonth;
}
echo $previous ->format('Y-m-d');

This code subtracts one month from the current date. If it is greater than the last day of the previous month we use the last day of the previous month instead.

Demo

Saturday, August 7, 2021
 
e_i_pi
 
5

This should work for you:

(Here I just used array_diff_key() to get the difference of the keys. The second array I flipped with array_flip() so to change the values to keys)

<?php

    $arr1 = array(
            "ABC01" => "10.123.456.78",
            "ABC02" => "10.123.456.79",
            "ABC03" => "10.123.456.80",
            "ZYX99" => "10.123.456.81"
    );

    $arr2 = array("ABC01", "ABC02", "ABC03");

    $result = array_diff_key ($arr1, array_flip($arr2));
    print_r($result);

?>

Output:

Array ( [ZYX99] => 10.123.456.81 )
Friday, August 20, 2021
 
icza
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :