【问题标题】:How to validate The current time is within the specified time period? [duplicate]如何验证当前时间是否在指定时间段内? [复制]
【发布时间】:2020-12-16 02:10:35
【问题描述】:

我在桌子上有 2 次​​p>

id start_date end_date
1 2020-12-14 16:56:0 2020-12-16 03:02:00

我使用 Carbon 检查当前时间是否在开始和结束时间之内?但它不起作用。

错误:预期为 null、字符串、DateTime 或 DateTimeInterface

$current = new Carbon();
$start = khuyenmai::select('ngaybd')->where('id_sp',1)->first();
$end = khuyenmai::select('ngaykt')->where('id_sp',1)->first();
var_dump($current->between($start,  $end));

如何检查当前日期是否在上述两个日期之间并获取布尔值 true 或 false?

【问题讨论】:

  • 你得到了一个非常清晰的错误信息,所以——去检查你正在使用的变量是否真的包含你认为它们应该包含的内容(也就是最基本的调试),看看它们的类型是否与什么兼容文档说。
  • 返回对象$start->ngaybd, $end->ngaykt

标签: php mysql laravel php-carbon


【解决方案1】:

你可以通过下一个简单的查询来检查:

select 'true' from tbl where now() between start_date and end_date and id = 1;

或 Laravel 风格:

$test = khuyenmai::select('ngaykt')
    ->where('id_sp',1)
    ->whereRaw('now() between start_date and end_date')
    ->first();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多