我一直试图找到一种方法来可靠地获取过去三个工作日的时间戳,无论输入日期和困难。我非常感谢任何建议。
这就是我的尝试:
$date = date() - 60*60*24*3;
$timestamps = array();
for($i=0; $i < 3; $i++){
$timestamps[] = strtotime($date . ' ' . $i . ' weekdays');
}
我正在寻找以下内容:
Input: lastThreeBusinessDays(date()); //Friday August 1 2014
Output (Array with timestamps for the following dates):
Thursday July 31 2014, Wednesday July 30 2014, Tuesday July 29 2014
Input: lastThreeBusinessDays(date()); //Saturday August 2 2014
Output (Array with timestamps for the following dates):
Friday August 1, Thursday July 31 2014, Wednesday July 30 2014
Input: lastThreeBusinessDays(date()); //Sunday August 3 2014
Output (Array with timestamps for the following dates):
Friday August 1, Thursday July 31 2014, Wednesday July 30 2014
非常感谢提前!
答案 0 :(得分:1)
时间戳具有秒粒度,因此获取一天的时间戳非常模糊。以下代码(假设工作日表示每天,但周六和周日)执行要求:
$timestamp = time();
$timestamps = array();
for ($n = 0; $n < 3; ++$n) {
// The following cycle skips saturdays and sundays
do {
$timestamp -= 60*60*24;
} while (date('N', $timestamp) > 5);
$timestamps[] = $timestamp;
}