php - Laravel Carbon 检查时间是否在同一分钟内

标签 php laravel datetime laravel-5

我有来自数据库的日期时间和来自 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/

相关文章:

php - 如何设计一个数据库来跟踪我家人拥有的书籍?

php - 如何将访问 token 附加到 Google 日历 API 请求

php - 使用 PHP 接收 JSON POST

PHP 回显空白日期时间字段

php - 未从 Laravel 中的原始查询返回任何结果

Laravel 5 维护模式无需工匠即可开启

Python Django 名称错误 : name 'datetime' is not defined

javascript - Laravel 无法从 Vue-multiselect 获取值

python-2.7 - 如何以 Day, Date Month Year HH :MM:SS? 格式打印当前时间

php - Carbon - 获取月份的第一天