我想计算两个给定日期之间所有星期日的分钟数。
function getWeekEnd($startDate, $endDate)
{
$working_hours = [
[0, 86400], // Sun
null,
null,
null,
null,
null,
null //Sat
];
$start = new \DateTime($startDate);
$end = new \DateTime($endDate);
$seconds = 0; // Total working seconds
// Calculate the Start Date (Midnight) and Time (Seconds into day) as Integers.
$start_date = clone $start;
$start_date = $start_date->setTime(0, 0, 0)->getTimestamp();
$start_time = $start->getTimestamp() - $start_date;
// Calculate the Finish Date (Midnight) and Time (Seconds into day) as Integers.
$end_date = clone $end;
$end_date = $end_date->setTime(0, 0, 0)->getTimestamp();
$end_time = $end->getTimestamp() - $end_date;
// For each Day
for ($today = $start_date; $today <= $end_date; $today += 86400) {
// Get the current Weekday.
$today_weekday = date('w', $today);
// Skip to next day if no hours set for weekday.
if (!isset($working_hours[$today_weekday][0]) || !isset($working_hours[$today_weekday][1])) continue;
// Set the office hours start/finish.
$today_start = $working_hours[$today_weekday][0];
$today_end = $working_hours[$today_weekday][1];
// Adjust Start/Finish times on Start/Finish Day.
if ($today === $start_date) $today_start = min($today_end, max($today_start, $start_time));
if ($today === $end_date) $today_end = max($today_start, min($today_end, $end_time));
// Add to total seconds.
$seconds += $today_end - $today_start;
}
$time = date('H:i', $seconds);
$hms = explode(":", $time);
return ($hms[0] + ($hms[1]/60));
}
目前我有这个,但是如果我赚了2019-11-22 22:00:00到2019-11-28 10:00:00我没有退货,您是否还有其他功能或修复了此功能?
非常感谢
答案 0 :(得分:0)
也许尝试这样的事情:
<?php
function getSundayMinutes($dateFromString, $dateToString)
{
$dateFrom = new \DateTime($dateFromString);
$dateTo = new \DateTime($dateToString);
$minutes = [
"total" => 0
];
if ($dateFrom > $dateTo) {
return $minutes;
}
if (1 != $dateFrom->format('N')) {
$dateFrom->modify('next sunday');
}
while ($dateFrom <= $dateTo) {
$minutes[$dateFrom->format('Y-m-d')] = 1440;
$minutes["total"] += 1440;
$dateFrom->modify('+1 week');
}
return $minutes;
}
$dateFromString = '2019-11-01';
$dateToString = '2019-11-19';
print_r(getSundayMinutes($dateFromString, $dateToString));
?>
由于11月是3个星期日,因此上面的代码将打印出来:
Array ( [total] => 4320 [2019-11-03] => 1440 [2019-11-10] => 1440 [2019-11-17] => 1440 )
我从这里php function for get all mondays within date range
使用了代码BR