Skip to content

Instantly share code, notes, and snippets.

@jnrbsn
Created September 14, 2010 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnrbsn/579651 to your computer and use it in GitHub Desktop.
Save jnrbsn/579651 to your computer and use it in GitHub Desktop.
Adds a specified number of business hours to a unix timestamp.
<?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