我已经尝试了很长一段时间来搜索我的问题的答案,但我还没有解决。我是PHP的新手。
问题。
将2:00除以2 (hr/2)
= 1:00 //格式h:i
感谢。
答案 0 :(得分:3)
我认为最安全的方法是转换为秒并使用日期显示它。
$time ="3:00";
list($hours, $minutes) = explode(":", $time);
$minutes += $hours*60;
$seconds = $minutes*60;
date_default_timezone_set ("UTC"); // makes sure there is no DST or timezone added to result
echo "new time: " . date("h:i", $seconds/2); // 01:30
你的问题是“h:i”格式,但它写成“1:00” 要获得1:00,您需要使用格式“G:i” https://3v4l.org/4MjVQ
答案 1 :(得分:1)
对于记录,在任何编程/脚本语言中划分时间都是微不足道的。因为如果我们谈论的是时间戳,那么划分它就没有意义,因为时间戳是年表的特定点。
另一方面,持续时间可以分开。但是,我不确定php的API是否有任何持续时间处理的实现。您仍然可以继续使用自己的定制实现来处理时间间隔。这将是类似的事情。
<?php
#We need at least two timestamps to get a duration
$time1 = new DateTime("2018-4-23 10:00:00");
$time2 = new DateTime("2018-4-23 11:00:00");
$durationInSeconds = ($time2->getTimestamp()) - ($time1->getTimestamp()); //Get the interval in seconds
#echo $durationInSeconds; // Gives you 3600 seconds
# Now you can divide these seconds into anything you prefer.
# Let's say I want two intervals. This means, I have to go for the timestamp in between the two. I could do that by simply adding half the amount of seconds
$halfDurationInSeconds = $durationInSeconds / 2;
$time1->add(new DateInterval("PT".$halfDurationInSeconds."S")); // adds 1800 secs
echo $time1->format('Y-m-d H:i:s');
答案 2 :(得分:0)
非常简单。使用strtotime
$your_time = "12:00";
date_default_timezone_set ("UTC");
$secs = strtotime($your_time ) - strtotime("00:00:00");
echo date("H:i:s",$secs / 2);
答案 3 :(得分:-1)
我想我们也可以在这里使用乘法。感谢上面的建议。它确实帮了我很多忙。这是我修改的上述建议代码。
function multiplyTime ($multiple, $prescribeWorkingHrs) {
list($hours, $minutes) = explode(":", $prescribeWorkingHrs);
$minutes += $hours*60;
$seconds = $minutes*60;
$product = $seconds * $multiple;
return date("h:i", $product);
}
结果:
multiplyTime(0.75, "8:00") = "6:00"
multiplyTime(0.5, "8:00") = "4:00"
multiplyTime(0.25, "8:00") = "2:00"
希望这也会有所帮助。感谢