3v4l.org

run code in 300+ PHP versions simultaneously
<?php ini_set('display_errors', 'on'); define('DAY_WORK', 32400); // 9 * 60 * 60 define('HOUR_START_DAY', '08:00:00'); define('HOUR_END_DAY', '17:00:00'); // get begin and end dates of the full period $date_begin = '2013-11-28 15:00:00'; $date_end = '2013-11-29 15:00:00'; // keep the initial dates for later use $d1 = new DateTime($date_begin); $d2 = new DateTime($date_end); // and get the datePeriod from the 1st to the last day $period_start = new DateTime($d1->format('Y-m-d 00:00:00')); $period_end = new DateTime($d2->format('Y-m-d 23:59:59')); $interval = new DateInterval('P1D'); //$interval = new DateInterval('weekdays'); // 1 day interval to get all days between the period $period = new DatePeriod($period_start, $interval, $period_end); $worked_time = 0; $nb = 0; // for every worked day, add the hours you want foreach($period as $date){ $week_day = $date->format('w'); // 0 (for Sunday) through 6 (for Saturday) if (!in_array($week_day,array(0, 6))) { // if this is the first day or the last dy, you have to count only the worked hours if ($date->format('Y-m-d') == $d1->format('Y-m-d')) { $end_of_day_format = $date->format('Y-m-d '.HOUR_END_DAY); $d1_format = $d1->format('Y-m-d H:i:s'); $end_of_day = new DateTime($end_of_day_format); $diff = $end_of_day->diff($d1)->format("%H:%I:%S"); $diff = split(':', $diff); $diff = $diff[0]*3600 + $diff[1]*60 + $diff[0]; $worked_time += $diff; } else if ($date->format('Y-m-d') == $d2->format('Y-m-d')) { $start_of_day = new DateTime($date->format('Y-m-d '.HOUR_START_DAY)); $d2_format = $d2->format('Y-m-d H:i:s'); $end_of_day = new DateTime($end_of_day_format); $diff = $start_of_day->diff($d2)->format('%H:%I:%S'); $diff = split(':', $diff); $diff = $diff[0]*3600 + $diff[1]*60 + $diff[0]; $worked_time += $diff; } else { // otherwise, just count the full day of work $worked_time += DAY_WORK; } } if ($nb> 10) die("die ".$nb); } echo sprintf('Works from %s to %s, You worked %d hour(s)', $date_begin, $date_end, $worked_time/60/60);
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Uncaught Error: Call to undefined function split() in /in/ogW8l:36 Stack trace: #0 {main} thrown in /in/ogW8l on line 36
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Deprecated: Function split() is deprecated in /in/ogW8l on line 36 Deprecated: Function split() is deprecated in /in/ogW8l on line 46 Works from 2013-11-28 15:00:00 to 2013-11-29 15:00:00, You worked 9 hour(s)

preferences:
188.37 ms | 402 KiB | 209 Q