Created
October 14, 2014 07:00
-
-
Save roboter/787e2361334c29fc28df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: roboter | |
* Date: 10.10.2014 | |
* Time: 14:12 | |
*/ | |
date_default_timezone_set( 'UTC' ); | |
require_once( 'tooaeg.php' ); | |
define ( 'NEXTDAY', 24 * 60 * 60 ); | |
$nighttime_end = strtotime( gmdate( 'd/m/Y', 0 ).' '.$SETTINGS_nighttime_end ); // 07:00 | |
$nighttime_start = strtotime( gmdate( 'd/m/Y', 0 ).' '.$SETTINGS_nighttime_start ); // 22:00 | |
echo '<pre>'; | |
foreach ( $EMPLOYEES as $employee ) { | |
echo 'Name: '.$employee['name']."\n"; | |
echo 'Shift start: '.$employee['shift_start']."\n"; | |
echo 'Shift end: '.$employee['shift_end']."\n"; | |
echo 'Interval: '.interval( date_create( $employee['shift_start'] ), date_create( $employee['shift_end'] ) )."\n"; | |
$start = strtotime( gmdate( 'd/m/Y', 0 ).' '.$employee['shift_start'] ); | |
$end = strtotime( gmdate( 'd/m/Y', 0 ).' '.$employee['shift_end'] ); | |
echo 'Daytime: '.intersection( | |
$start, | |
$end, | |
$nighttime_end, | |
$nighttime_start)."\n"; | |
if ( $start > $end ) { | |
$end = $end + NEXTDAY; | |
} | |
echo 'Nighttime: '.intersection( | |
$start, | |
$end, | |
$nighttime_start, | |
$nighttime_end + NEXTDAY )."\n"; | |
echo "\n"; | |
} | |
echo '</pre>'; | |
function interval( $start, $end ) { | |
$start_unix = $start->getTimestamp(); | |
$end_unix = $end->getTimestamp(); | |
if ( $end_unix > $start_unix ) { | |
return gmdate( 'H:i', $end_unix - $start_unix ); | |
} | |
return gmdate( 'H:i', 0 - $start_unix + $end_unix ); | |
} | |
function intersection( $start, $end, $start_shift, $end_shift ) { | |
// print_time($start); | |
// print_time($end); | |
if ( $start_shift > $end || $start > $end_shift || $end < $start || $end_shift < $start_shift ) { | |
return '-'; | |
} | |
$start = $start < $start_shift ? $start_shift : $start; | |
$end = $end < $end_shift ? $end : $end_shift; | |
return gmdate( 'H:i', $end - $start ); | |
} | |
function print_time( $time ) { | |
echo gmdate( 'H:i', $time ).( gmdate( 'd', $time ) == 2 ? ' + day' : '' )."\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment