- date: documentation ( source)
- strtotime: documentation ( source)
<?php
function isWeekend($date)
{
if ($date instanceof DateTime) {
$date = DateTimeImmutable::createFromMutable($date);
}
return $date->setTime(0,0,0) != $date->modify('this weekday');
}
function isWeekendUnixTimestamp($dateValue)
{
return date('Yz', strtotime($dateValue)) != date('Yz', strtotime($dateValue . ' this weekday'));
}
echo 'DateTimeInterface Comparison:' . PHP_EOL;
$sunday = new DateTimeImmutable('Sunday');
$periods = new DatePeriod($sunday, new DateInterval('P1D'), 6);
foreach ($periods as $date) {
echo $date->format('D') . ' is' . (isWeekend($date) ? '' : ' not') . ' a weekend' . PHP_EOL;
}
echo PHP_EOL . 'String Comparison:' . PHP_EOL;
foreach ($periods as $date) {
echo $date->format('D') . ' is' . (isWeekendUnixTimestamp($date->format('Y-m-d')) ? '' : ' not') . ' a weekend' . PHP_EOL;
}