我正在尝试计算$ work_in_daytime和$ work_in_nighttime。
$nightStart = '22:00';
$nightEnd = '07:00';
$workers = array(
'0' => array(
'name' => 'Lyons',
'start' => '15:15',
'end' => '23:45'
),
'1' => array(
'name' => 'Santos',
'start' => '10:00',
'end' => '22:00'
),
'2' => array(
'name' => 'Montgomery',
'start' => '22:30',
'end' => '08:00'
)
);
我尝试过以下代码。
foreach ($workers as $worker) {
$startTime = strtotime($em['start']);
$endTime = strtotime($em['end']);
$length = formattedTime($endTime - $startTime);
$nightTimeStart = strtotime($nightStart);
$nightTimeEnd = strtotime($nightEnd);
$dayTimeLength = 0;
$nightTimeLength = 0;
if ($startTime > $nightTimeStart) {
$nightTimeLength += $startTime - $nightTimeStart;
}
if ($startTime < $nightTimeStart) {
$dayTimeLength += $nightTimeStart - $startTime;
}
if ($endTime > $nightTimeStart) {
$nightTimeLength += $endTime - $nightTimeStart;
}
if ($endTime < $nightTimeStart) {
$dayTimeLength += $endTime - $nightTimeStart;
}
echo 'Day Time: '.$dayTimeLength.PHP_EOL;
echo 'Night Time: '.$nightTimeLength.PHP_EOL;
}
我坚持正确的计算。
答案 0 :(得分:1)
试试这个:
$nightStart = '22:00';
$nightEnd = '07:00';
$workers = array(
'0' => array(
'name' => 'Lyons',
'start' => '15:15',
'end' => '23:45'
),
'1' => array(
'name' => 'Santos',
'start' => '6:00',
'end' => '22:00'
),
'2' => array(
'name' => 'Montgomery',
'start' => '22:30',
'end' => '08:00'
)
);
function timetodate($time)
{
return date('H:i', $time);
}
foreach ($workers as $worker) {
$startTime = strtotime($worker['start']);
$endTime = strtotime($worker['end']);
$length = date('H:i',($endTime - $startTime));
$day = strtotime("24H");
$nightTimeStart = strtotime($nightStart);
$nightTimeEnd = strtotime($nightEnd);
$dayTimeLength = 0;
$nightTimeLength = 0;
/* Night Time Calculations */
if ($endTime <= $startTime) {
if ($startTime >= $nightTimeStart) {
$nightTimeLength += $day - $startTime;
if ($endTime <= $nightTimeEnd) {
$nightTimeLength += $nightTimeEnd - $endTime;
}
else
{
$nightTimeLength += $nightTimeEnd;
}
}
}
elseif ($endTime > $nightTimeStart) {
$nightTimeLength += $endTime - $nightTimeStart;
}
elseif ($startTime < $nightTimeEnd) {
$nightTimeLength += $nightTimeEnd - $startTime;
}
/* Day Time Calculations */
if ($startTime >= $nightTimeEnd) {
if ($endTime <= $nightTimeStart) {
if ($startTime < $endTime) {
$dayTimeLength += $endTime - $startTime;
}
else
{
$dayTimeLength += $endTime - $nightTimeEnd;
}
}
else
{
$dayTimeLength += $nightTimeStart - $startTime;
}
}
else
{
if ($endTime <= $nightTimeStart) {
$dayTimeLength += ($endTime - $nightTimeEnd);
}
else
{
$dayTimeLength += $nightTimeStart - $nightTimeEnd;
}
}
echo "Name:". $worker['name'].PHP_EOL;
echo 'Day Time: '.timetodate($dayTimeLength).PHP_EOL;
echo 'Night Time: '.timetodate($nightTimeLength).PHP_EOL.PHP_EOL;
}
https://www.tehplayground.com/CkZ53jjD1cyFEymB
早先的帖子: 无视---- https://www.tehplayground.com/PohT0L7s35m9VKOe
喜欢这个? 虽然我没有对它进行调整,但我认为我需要更改一点,以便在新的一天之前正确地进行调整(它不会将其视为一种我认为不会持续的转变)
答案 1 :(得分:0)
$em
未定义。<?php
$start_nighttime = strtotime('18:00');
$end_nighttime = strtotime('05:59');
$start_daytime = strtotime('06:00');
$end_daytime = strtotime('17:59');
foreach($worker as $data){
$worker_start = strtotime($data['start']);
$worker_end = strtotime($data['end']);
if ($worker_start > $start_daytime && $worker_end < $start_nighttime) {
//Worked only during the day
$daytime = round(abs($worker_start - $worker_end) / 60, 2);
} elseif ($worker_start > $start_daytime && $worker_end > $end_nighttime) {
//Worked during the day and extended past defined night time
$daytime = round(abs($worker_start - $end_daytime) / 60, 2);
$nighttime = round(abs($start_nightime - $worker_end) / 60, 2);
} elseif ($worker_start > $start_nighttime && $worker_end < $end_nighttime){
//Worked only during the night
$nighttime = round(abs($worker_start - $worker_end));
} elseif ($worker_start > $start_nighttime && $worker_end > $end_nighttime){
//Worked only during the night up until the moring
$nighttime = round(abs($worker_start - $end_nighttime));
$daytime = round(abs($start_daytime - $worker_end));
}
}
?>