【问题标题】:Date_diff() expects parameter 2 to be DateTimeInterface, Boolean GivenDate_diff() 期望参数 2 为 DateTimeInterface, Boolean Given
【发布时间】:2020-02-03 21:23:08
【问题描述】:

我创建了一个函数,我想检查两个日期之间是否有一定的周数。

public function checkShifts()
    {
        $now = new DateTime();
        $shifts = Shift::where("disabled", "=", "0")->get();

        foreach ($shifts as $shift)
        {
            $time = DateTime::createFromFormat('Y-m-d H:MMM:SS', $shift->start, null);

            if (date_diff($now, $time, false) >= 42)
            {
                $shift->disabled = true;
            }
        }
    }

我得到 Shift 开始日期,将其转换为 DateTime 对象并尝试运行 date_diff。但是,我不断收到 Date_diff() 期望参数 2 为 DateTimeInterface, Boolean Given 错误。

VAR_DUMP:

object(App\Shift)#729 (26) {
  ["table":protected]=>
  string(5) "shift"
  ["connection":protected]=>
  string(5) "mysql"
  ["primaryKey":protected]=>
  string(2) "id"
  ["keyType":protected]=>
  string(3) "int"
  ["incrementing"]=>
  bool(true)
  ["with":protected]=>
  array(0) {
  }
  ["withCount":protected]=>
  array(0) {
  }
  ["perPage":protected]=>
  int(15)
  ["exists"]=>
  bool(true)
  ["wasRecentlyCreated"]=>
  bool(false)
  ["attributes":protected]=>
  array(7) {
    ["id"]=>
    int(21)
    ["start"]=>
    string(19) "2020-02-03 15:00:00"
    ["end"]=>
    string(19) "2020-02-03 21:00:00"
    ["department_id"]=>
    int(1)
    ["disabled"]=>
    int(0)
    ["created_at"]=>
    string(19) "2020-02-03 15:41:06"
    ["updated_at"]=>
    string(19) "2020-02-03 20:39:27"

【问题讨论】:

  • 在您的foreachvar_dump($shift) 中;以确保它包含您所期望的内容。
  • 如果$shift 是一个数组,那么你不应该这样做$shift->start
  • DateTime::createFromFormat 将在失败时返回 false,因此您的格式字符串不正确或 $shift->start 不正确。
  • 另外,您认为“H:MMM:SS”在日期格式中代表什么?因为我几乎可以保证它不是你想的那样。查看日期格式字符here
  • 你想要的格式是 "Y-m-d H:i:s" 投票结束作为错别字

标签: php laravel


【解决方案1】:

DateTime::createFromFormat 在失败时返回 false。

在我看来 Y-m-d H:MMM:SS 的格式不正确。 Y-m-d H:i:s 将是 2020-02-03 21:40:20

【讨论】:

    【解决方案2】:

    正如 Lee 所说,它的格式错误导致格式化日期为布尔值...

    你可以做的是在模型中将 start 属性转换为 datetime 并有这样的东西:

    protected $casts = [
        'start' => 'datetime',
        ...
    ];
    

    然后你就可以做这样的事情了:

    public function checkShifts()
    {
        $shifts = Shift::where("disabled", "=", "0")->get();
    
        foreach ($shifts as $shift)
        {
            if ($shift->start->diffInWeeks(now()) >= 42)
            {
                $shift->disabled = true;
            }
        }
    
        return $shifts;
    }
    

    使用集合的map方法:

    public function getShifts()
    {
        return Shift::where("disabled", "0")->get()->map(function ($shift) {
            $shift->disabled = (bool)$shift->start->diffInWeeks(now()) >= 42;
    
            return $shift;
        })->values();
    }
    

    您可以使用集合的过滤方法来仅获取启用的班次:

    public function enabledShifts()
    {
        return Shift::where("disabled", "0")->get()->filter(function ($shift) {
            return $shift->start->diffInWeeks(now()) >= 42;
        })->values();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 2018-03-23
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      相关资源
      最近更新 更多