taskTimeShift - 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 taskTimeShift.php

  1. <?php
  2. define('REPORT_GEN_MIN', 3600); // 1 hour
  3. define('REPORT_GEN_MAX', 9000); // 2,5 hours
  4. define('ONE_DAY', 86400); // 1 day in secs
  5.  
  6. function taskTimeShift($ts = 0, $minShift = REPORT_GEN_MIN, $maxShift = REPORT_GEN_MAX) {
  7.  
  8.     $ts = $ts $ts : time(); // Now
  9.     
  10.     $shift = rand($minShift, $maxShift); // Shift delta
  11.     $tsGen = $ts + $shift; // shif timestamp
  12.     $h = (int)date('H', $tsGen);
  13.     //echo " $h | ";
  14.     
  15.     if ($h >= 19) { // after worktime
  16.       $tsGen = strtotime(date('Y-m-d 08:30:00', $ts + ONE_DAY)) - $minShift; // On the next day at 8:30 o'clock
  17.       $tsGen += $shift; // +1...3 hours (in sec)
  18.     }
  19.     elseif ($h < 9) { // before work time
  20.       $tsGen = strtotime(date('Y-m-d 08:30:00', $ts)) - $minShift; // On this day as 8:30 o'clock
  21.       if ($tsGen < $ts) $tsGen += ONE_DAY;
  22.       $tsGen += $shift; // +1...3 hours (in sec)
  23.     }
  24.     
  25.     return $tsGen;
  26.     /*return [
  27.       'ts' => $tsGen,
  28.       'shift' => $shift
  29.     ]; */
  30. }
  31.  
  32. $ts = time()-10000;//+3600*3;
  33. for ( $i=0; $i < 1000; $i ++) {
  34.     //echo date('H:i:s  d.m.Y', taskTime($tsShift)). "\n";
  35.     
  36.     $tsShift = taskTimeShift($ts);
  37.     echo date('  H:i   d   .m.Y', $ts);
  38.     echo date('  -->  H:i   d   .m.Y', $tsShift);
  39.     printf("  ___ %.1f", ($tsShift - $ts)/3600.0);
  40.     
  41.     echo "\n";
  42.       
  43.     $ts+=60*5;
  44.     
  45. }
File Description
  • taskTimeShift
  • PHP Code
  • 24 Oct-2019
  • 1.29 Kb
You can Share it: