Print Y-m-d list of business dates between two dates from MySQL using PHP

白昼怎懂夜的黑 提交于 2019-12-02 13:51:09
// Date strings from DB
$startDate = '2012-08-01';
$endDate = '2012-08-08';

// Convert to UNIX timestamps
$currentTime = strtotime($startDate);
$endTime = strtotime($endDate);

// Loop until we reach the last day
$result = array();
while ($currentTime <= $endTime) {
  if (date('N', $currentTime) < 6) {
    $result[] = date('Y-m-d', $currentTime);
  }
  $currentTime = strtotime('+1 day', $currentTime);
}

// Show the result
// You could loop the array to pretty-print it, or do it within the above loop
print_r($result);

See it working

This will print the range of dates:

$startDate = '2012-08-01';
$endDate = '2012-08-08';

$date = new DateTime($startDate);
while ($date->format('Y-m-d') != $endDate) {

    if ($date->format('N') > 5) {
        $date->modify('+1 day');
        continue;
    }

    echo $date->format('Y-m-d') . PHP_EOL;
    $date->modify('+1 day');
}
echo $endDate;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!