Call us Today +1 630 534 0220
Hello Folks,
We are proud to announce implementation PHP function to add working hours to date excluding holidays.


<?php

$dayStart =9;

$dayEnd = 17;

$timestamp = strtotime(’01-04-2015 14:10:00′);

$hoursToAdd = 6;

$holidays = array

(

‘Apr 3′,

‘Apr 9′,

‘Apr 10′

);

$timestamp = addWorkingHours1($dayStart,$dayEnd,$timestamp,$hoursToAdd,$holidays,true);

echo $php_timestamp_date = date(“m-d-Y H:i:s”, $timestamp);

function addWorkingHours1($dayStart,$dayEnd,$timestamp, $hoursToAdd,$holidays,$skipWeekends = false)

{

// CONVERT HOLIDAYS TO ISO DATES

foreach ($holidays as $x => $holiday)

{

$holidays[$x] = date(‘Y-m-d’, strtotime($holiday));

}

// For every hour to add

for($i = 0; $i < $hoursToAdd; $i++) { $iso_date = date(‘Y-m-d’, $timestamp); if (!in_array($iso_date, $holidays)) { // Add the hour $timestamp += 3600; // If the time is between 1800 and 0800 if ((date(‘G’, $timestamp) >= $dayEnd && date(‘i’, $timestamp) >= 0 ) || (date(‘G’, $timestamp) < $dayStart)) { // If on an evening if (date(‘G’, $timestamp) >= $dayEnd)

{

// Skip to following morning at 08XX

$timestamp += 3600 * ((24 – date(‘G’, $timestamp)) + $dayStart);

}

// If on a morning

else

{

// Skip forward to 08XX

$timestamp += 3600 * ($dayStart – date(‘G’, $timestamp));

}

}

// If the time is on a weekend

if ($skipWeekends && (date(‘N’, $timestamp) == 6 || date(‘N’, $timestamp) == 7))

{

// Skip to Monday

$timestamp += 3600 * (24 * (8 – date(‘N’, $timestamp)));

}

$iso_date = date(‘Y-m-d’, $timestamp);

if (in_array($iso_date, $holidays))

{

$timestamp += 3600 * (24 * (8 – date(‘N’, $timestamp)));

}

}

else

{

//$timestamp += 3600;

$timestamp += 3600 * (24 * (8 – date(‘N’, $timestamp)));

}

}

// Return

return $timestamp;

}

?>



Do You Need more information ?


For any further information / query regarding Technology, please email us at info@varianceinfotech.com
OR call us on +1 630 861 8263, Alternately you can request for information by filling up Contact Us
facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Leave a Reply

Your email address will not be published. Required fields are marked *

*