Created
September 14, 2010 19:46
-
-
Save jnrbsn/579651 to your computer and use it in GitHub Desktop.
Adds a specified number of business hours to a unix timestamp.
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 | |
/** | |
* Adds a specified number of business hours to a unix timestamp. | |
* | |
* @param int $time_start The timestamp at which to start | |
* @param int $plus_hours The number of business hours to add | |
* | |
* @return int The resulting timestamp | |
*/ | |
function plus_business_hours($time_start, $plus_hours) | |
{ | |
$time_end = $time_start; | |
$time_info = getdate($time_end); | |
$seconds = ($time_info['hours'] * 3600) | |
+ ($time_info['minutes'] * 60) | |
+ $time_info['seconds']; | |
$time_end += ($time_info['hours'] < 9) ? 32400 - $seconds : 0; | |
$time_end += ($time_info['hours'] >= 18) ? 118800 - $seconds : 0; | |
$time_end += ($time_info['hours'] == 13) ? 50400 - $seconds : 0; | |
for ($i = 0; $i < $plus_hours; ++$i) { | |
do { | |
$time_end += 3600; | |
$time_info = getdate($time_end); | |
} while ($time_info['hours'] < 9 | |
|| $time_info['hours'] >= 18 | |
|| $time_info['hours'] == 13 | |
|| $time_info['wday'] == 0 | |
|| $time_info['wday'] == 6 | |
); | |
} | |
return $time_end; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment