我有来自数据库的日期时间和来自 input='time'
字段的日期时间。
我有一个不带秒的日期时间(因此默认为 00)和一个带秒的日期时间。我需要检查这两个变量是否在同一分钟内。
示例:为 True
Carbon {#349 ▼
+"date": "2017-11-06 14:47:00.000000"
+"timezone_type": 3
+"timezone": "America/Chicago"
}
Carbon {#362 ▼
+"date": "2017-11-06 14:47:44.000000"
+"timezone_type": 3
+"timezone": "America/Chicago"
}
示例:对于 False
Carbon {#349 ▼
+"date": "2017-11-06 14:48:00.000000"
+"timezone_type": 3
+"timezone": "America/Chicago"
}
Carbon {#362 ▼
+"date": "2017-11-06 14:47:44.000000"
+"timezone_type": 3
+"timezone": "America/Chicago"
}
我现在正在执行此操作,但它将在 60 秒内显示。我很难显示它是否在同一分钟内。
$InsertTime = 2017-11-06 14:47
$DbTime = 2017-11-06 14:47:44
public function WithinMinCheck($InsertTime, $DbTime) {
$WithinMin = "true";
$InsertTime = Carbon::createFromFormat('Y-m-d H:i', $InsertTime);
$DbTime = Carbon::createFromFormat('Y-m-d H:i:s', $DbTime);
$difference = $InsertTime->diffInSeconds($DbTime);
if ($difference >= 60) {
$WithinMin = "false";
} else {
$WithinMin = "true";
}
return $WithinMin;
}
最佳答案
能否将第二个时间变量的第二个字段设置为 0 并比较这些值?
$newTime = $DbTime->copy()->second(00);
if($InsertTime->ne($newTime)) {
$WithinMin = "false";
}
return $WithinMin; //since $withinMin is set to true initially, it would only be false if Insertime is not equal
请记住,上述解决方案仅在分钟值相同时才有效。如果第二个时间具有不同的分钟值,即使它在第一个时间的 60 秒之内,也会返回 false。
关于php - Laravel Carbon 检查时间是否在同一分钟内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47147400/