Comparison between two intervals - 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.

Name: Comparison between two intervals fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of Comparison between two intervals.php

  1. <?php
  2.             // Time Test
  3.             $nowTime = time();
  4.             $time1 = date('H:i:s', strtotime($nowTime));
  5.             $time2 = date('H:i:s', strtotime('05:30'));
  6.  
  7.             // Day interval
  8.             $dayStartStr = date('H:i:s', strtotime('06:00:00'));
  9.             $dayStartInt = DateTime::createFromFormat('Y-m-d H:i:s', $dayStartStr);
  10.             $dayEndStr = date('H:i:s', strtotime('+1 day', strtotime('21:59:00')));
  11.             $dayEndInt = DateTime::createFromFormat('Y-m-d H:i:s', $dayEndStr);
  12.  
  13.             // Night interval
  14.             $nightStartStr = date('H:i:s', strtotime('05:59:00'));
  15.             $nightStartInt = DateTime::createFromFormat('Y-m-d H:i:s', $nightStartStr);
  16.             $nightEndStr = date('H:i:s', strtotime('+23 hour 59 minutes 59 seconds', strtotime('22:00:00')));
  17.             $nightEndInt = DateTime::createFromFormat('Y-m-d H:i:s', $nightEndStr);
  18.             
  19.             /*$interval = $nightStartInt->diff($nightEndInt);
  20.             $total = $interval->format("%H:%I");
  21.             echo $total;*/
  22.             
  23.             if($time2 >= $dayStartStr && $time2 <= $nightStartInt) {
  24.                 echo 'First interval!';
  25.             } else if ($time2 >= $nightStartStr && $time2 <= $dayStartInt) {
  26.                 echo 'Second interval!';
  27.             }
  28. ?>
  29.  
File Description
  • Comparison between two intervals
  • PHP Code
  • 13 Jan-2020
  • 1.27 Kb
You can Share it: