简体   繁体   English

在php和laravel 24H中减去两次

[英]subtract two times in php and laravel 24H

This seems like a fairly simple question, but I'm having trouble with it!这似乎是一个相当简单的问题,但我遇到了麻烦! In my request, I have two fields在我的请求中,我有两个字段

$takeOffTime = '23:00';
$landingTime = '01:05';

I want to subtract breakout from break in to get the difference, and then take that number and subtract it from the difference between takeOffTime and landingTime.我想从突破中减去突破以获得差异,然后取该数字并从 takeOffTime 和着陆时间之间的差异中减去它。

How can I do this in 24H formt 'h:i '?如何以 24H 格式“h:i ”执行此操作?

$takeOff = '23:00';
$landing = '01:10';

$t1 = strtotime($takeOff);
$t2 = strtotime($landing);

You can subtract two times to H:i format like this.您可以像这样将H:i格式减去两次。

$takeOff = '23:00';
$landing = '01:10';

$t1 = strtotime($takeOff);
$t2 = strtotime($landing);

$diff = gmdate('H:i', $t2 - $t1);

dd($diff); // "02:10"

With DateTime diff you can calculate the DateInterval, then format the DateInterval with format,使用 DateTime diff,您可以计算 DateInterval,然后使用格式格式化 DateInterval,

$takeOff = '23:00';
$landing = '01:05';
$take_off_date = new DateTime('23:00');
$landing_date = new DateTime('01:05');
if($landing <= $takeOff){
    $landing_date->add(new DateInterval("P1D"));
}
echo ($landing_date->diff($take_off_date))->format("%H:%I");

output 02:05输出02:05

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM