Determine if a specific Date and Time in within a day of week or time of day.

Here is a PHP function that will return true of false if a specific DateTime object falls within the day of week of two DateTime objects, down to the second. For example, you want to know if December 31, 2017 14:52:34 is between a Sunday at 12 AM and Sunday at 11PM?

To answer that question correctly, you can not simply rely on just comparing if the given date is between the start and end of the interval.

Note: Note: Set your timezone accordingly with date_default_timezone_set('???');. Where "???" is the timezone you are working with.

/**
*
* Returns true if the given date is within the range provided based on the
* $datetime_component given.
*
* For example, if the $datetime_component is 'w', this tests if the 'w' component
* of the given $date is between the 'w' component of both $start and $end
*
* @param type $date
* @param \DateTime $start
* @param \DateTime $end
* @param Array $datetime_component formatting char to test against.  Granularity should be from largest to smallest. (ie. day, hour, minute, second)
*/
function date_in_period(\DateTime $date, \DateTime $start, \DateTime $end, $datetime_component = ['w', 'H', 'i', 's']){
// Shift off a component of the DateTime to test
$component = array_shift($datetime_component);

// Stop if there are no components left to test
if(empty($component)){
return false;
}

// Get the value of the DateTime component for the given DateTime object
$specimen = $date->format($component);
$start_component = $start->format($component);
$end_component = $end->format($component);

// Test this with the DateTime component of the start and end DatTime objects
if(
($specimen >= $start_component && $specimen <= $end_component) ||
($specimen == $start_component) ||
($specimen == $end_component)
){
// It's falls between the range or matches one of the start or end ranges
return true;
}
else{
// Check the remaining components
date_in_period($date, $start, $end, $datetime_component);
}
}

The above can be used as a basis to answer the question... "Is this a weekend time?".

$string_time_start = '';
$string_time_end = '';

// Add your weekend hours here.  You can indicate by specific day down to the second, or by range spanning multiple days.
$exclude_day_times_string = array(
array('start' => 'Friday 17:00', 'end' => 'Friday 23:59:59'),
array('start' => 'Saturday 00:00', 'end' => 'Monday 08:00'),
);

$start_time = new DateTime($string_time_start);
$end_time = new DateTime($string_time_end);

$exclude_day_times = array();
foreach($exclude_day_times_string as $exclusion_string){
$exclude_day_times[] = array('start' => new DateTime($exclusion_string['start']), 'end' => new DateTime($exclusion_string['end']));
}

$period = new DatePeriod($start_time, new DateInterval('PT1S'), $end_time);

// Look at each second
foreach ($period as $second) {
// Does this period exist within any of the exclusion ranges
foreach($exclude_day_times as $exclusion){
$is_excluded = date_in_period($second, $exclusion['start'], $exclusion['end']);
}
if($is_excluded){
// Do what you need to do for weekend hours
}
else{
// Do what you need to do for non-weekend hours
}
}
Pacy
Powered by Pacy