我正在开发一个围绕日期的Web应用程序.
我需要根据天数来计算数字,例如 - 伪代码
$count_only = array('monday', 'wednesday', 'friday'); //count only these days
$start_date = 1298572294; // a day in the past
$finish_date = 1314210695; //another day
$var = number_of_days_between($start_date, $finish_date, $count_only);
Run Code Online (Sandbox Code Playgroud)
有没有办法确定已经过了多少整天,而只计算某些天?
您可以创建一个循环,该循环从 开始进入数组中的第二天$count_only,$start_date并在到达 时停止(从函数返回)$end_date。
function number_of_days_between($start_date, $finish_date, $count_only) {
$count = 0;
$start = new DateTime("@$start_date");
$end = new DateTime("@$finish_date");
$days = new InfiniteIterator(new ArrayIterator($count_only));
foreach ($days as $day) {
$count++;
$start->modify("next $day");
if ($start > $end) {
return $count;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1642 次 |
| 最近记录: |