我有以下格式日期时间:
开始:2017年7月16日20:00
结束:2017年7月16日23:30PHP多少小时都在一定的时间间隔
开始:2017年-07-18 21:30
结束于:2017-07-19 00:30
我需要从这些时间间隔中知道在18:00-22:00之间花了多少小时(以0.5个增量)和22:00-06:00共计一个月。
在此先感谢您的任何提示。
当前的代码我是这样的,但我不知道这是否是涵盖了所有的时间内可能:
<?php
date_default_timezone_set("UTC");
$start = array(
"2017-07-16 21:30:00",
"2017-07-16 18:00:00"
);
$end = array(
"2017-07-17 00:30:00",
"2017-07-16 18:30:00"
);
$amount_low = 0;
$amount_high = 0;
for($i = 0; $i < sizeof($start); $i++) {
$start_time = date("H:i:s", strtotime($start[$i]));
$end_time = date("H:i:s", strtotime($end[$i]));
$start_date = date("Ymd", strtotime($start[$i]));
$end_date = date("Ymd", strtotime($end[$i]));
// getting chunk before 22:00 if
if(
(strtotime($start[$i]) >= strtotime($start_date . " 18:00") && strtotime($start[$i]) < strtotime($start_date . " 22:00"))
&&
$start_date < $end_date
) {
$interval_low = strtotime($start_date . " 22:00") - strtotime($start[$i]);
$amount_low += ceil($interval_low/1800)/2;
}
//amount_high
if(strtotime($start[$i]) > strtotime($start_date . " 22:00") && strtotime($start[$i]) < strtotime($start_date . " 06:00")) {
$interval_high = strtotime($end[$i]) - strtotime($start[$i]); //needs further things
$amount_high += ceil($interval_high/1800)/2;
} elseif (strtotime($start[$i]) < strtotime($start_date . " 22:00") && strtotime($end[$i]) > strtotime($start_date . " 22:00")) {
$interval_high = strtotime($end[$i]) - strtotime($start_date . " 22:00");
$amount_high += ceil($interval_high/1800)/2;
} else {
$interval_low = strtotime($end[$i]) - strtotime($start[$i]);
$amount_low += ceil($interval_low/1800)/2;
}
}
echo $amount_low;
echo "\n$amount_high";
?>
你能提供一些代码和预期的输出吗?是第一个例子2小时和1.5小时的答案? – Andreas
@Andreas是的,第一个例子确实应该提供上述金额。我试图实现的代码与下面两个日期之间的时间差异相同。 – ediets
我们要求代码的原因是因为我们想知道您尝试了什么。而不必从头开始。并使用相同的变量,以便您不必将它们更改为变量。 – Andreas