timeDiffs - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of timeDiffs.php

  1. <?php
  2. function counter($time, $modify = '+1 day') {
  3.     $date = new DateTime($time);
  4.     $date->modify($modify);
  5.     $diff = $date->diff(new DateTime($time));
  6.     $h = ($diff->days > 0 ? $diff->days * 24 : 0) + $diff->h;
  7.     $i = $diff->i;
  8.     $s = $diff->s;
  9.     return "$h:{$diff->i}:{$diff->s}";
  10. }
  11. function timeAdd($add, $time = "00:00:00") {
  12.     list($h1, $i1, $s1, $h2, $i2, $s2) = explode(':', $time.":".$add);
  13.     $s = $s1 + $s2;
  14.     $i = $s > 59 ? intdiv($s, 60) : 0;
  15.     $s = $s > 59 ? $s % 60 : $s;
  16.     
  17.     $i += $i1 + $i2;
  18.     $h = $i > 59 ? intdiv($i, 60) : 0;
  19.     $i = $i > 59 ? $i % 60 : $i;
  20.     
  21.     $h += $h1 + $h2;
  22.     return "$h:$i:$s";
  23. }
  24. function timeAddMulti() {
  25.     $args = func_get_args();
  26.     $start = "00:00:00";
  27.     for ($i = 0; $i < count($args); $i++)
  28.         $start = timeAdd($args[$i], $start);
  29.     return $start;
  30. }
  31.  
  32. $time = "15.06.2018 13:32";
  33. echo timeAdd("23:59:23", "22:59:23");
File Description
  • timeDiffs
  • PHP Code
  • 15 Jun-2018
  • 911 Bytes
You can Share it: