大家好,我可以帮助我完成我的项目,我在计算两个日期之间的渲染时间时遇到问题。具体来说,我的问题是在晚上10点到早上6点之间计算员工的夜间差异
e.g1: If the employee time in is 2017-01-01 21:00 (9pm) and the time out is 2017-01-02 05:00 (5am) the output will be: 7hours.
e.g.2: time in = 2017-01-01 17:00 (5pm) and time out = 2017-01-02 02:00 (2am) the output will be: 4 hours.
e.g.3: timein = 2017-01-01 02:00 (2am) and time out = 2017-01-01 10:00 (10am) the output will be: 4 hours.
如果日志在晚上10点到早上6点之间通过,则计数将开始
请提前帮助你。
答案 0 :(得分:0)
下面:
<?php
$output = abs((strtotime("2017-01-01 21:00") - strtotime("2017-01-02 05:00")) / 3600);
echo $output; // Outputs 8
答案 1 :(得分:0)
您可以使用unix时间:
$Date1 = strtotime(date("2017-01-01 17:00"));
$Date2 = strtotime(date("2017-01-02 02:00"));
$hours = ($Date2 - $Date1) / 3600;
答案 2 :(得分:0)
您可以使用DateTime函数:
int length(const int& value) noexcept {
//if (std::is_integral<int>::value) { this branch is taken
return value;
//else discarded
// return value.length(); discarded
}
std::size_t length(const std::string& value) noexcept {
//if (std::is_integral<int>::value) { discarded
// return value; discarded
//else this branch is taken
return value.length();
}
输出:
<?php
$date1 = new DateTime("2017-01-01 21:00");
$date2 = new DateTime("2017-01-02 05:00");
if (($date1->format('H') >= 22) && (($date2->format('H') <= 6))) {
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%h hours');
} else {
echo "0 hours";
}
文档:http://php.net/manual/en/datetime.diff.php和http://php.net/manual/en/datetime.format.php
答案 3 :(得分:0)
function computeNightDifferential($time_in, $time_out)
{
$start = "2016-01-01 22:00:00"; // 10pm
$end = "2016-01-02 06:00:00"; // 6am
$time_in = "2016-01-01 23:00:00";
$time_out = "2016-01-02 06:30:00";
$night_diff = 0;
if($time_out > $start AND $time_out <= $end AND $time_in <= $start){
$night_diff = abs(strtotime($start) - strtotime($time_out)) / 3600;
}else if($time_out <= $end AND $time_in > $start){
$night_diff = abs(strtotime($time_in) - strtotime($time_out)) / 3600;
}else if($time_out > $end AND $time_in > $start){
$night_diff = abs(strtotime($time_in) - strtotime($end)) / 3600;
}else if($time_out > $end AND $time_in <= $start){
$night_diff = abs(strtotime($start) - strtotime($end)) / 3600;
}
echo $night_diff;
}
感谢您的帮助!