【问题标题】:How to get min date from Laravel collection如何从 Laravel 集合中获取最小日期
【发布时间】:2019-10-30 23:52:45
【问题描述】:

我收藏了promotions,每次推广都有很多exceptions

foreach ($promotions as $p) {
    $t = $p->exceptions()
            ->where('sunday','on')
            ->whereDate('start','<=', $c_date)
            ->whereDate('end','>=', $c_date)
            ->get()
            ->sortBy('promotion_id');

    echo $t->min('start'). ' from: '.$t->min('start_time') .' ';
    echo $t->max('end') . ' to: '.$t->max('end_time').'</br>';
}

因为促销有很多例外情况,我需要知道每个促销的最短日期和最长日期。

上面的代码运行良好,但它使用'start'作为字符串而不是日期,结果无效。

如何从集合中获取最小日期和最大日期。

【问题讨论】:

  • startend 存储为什么数据类型?
  • 从数据库中获取您的开始日期和结束日期列并使用它来解析它。 date('D M, Y', strtotime())
  • 您是否将日期定义为日期?检查date mutators

标签: php laravel date min php-carbon


【解决方案1】:

理想情况下,最好为每个开始和结束设置一个 DATETIME 字段,而不是将日期和时间分开(这可能会导致错误)。但如果你真的想要分离:

  • 确保startend在数据库中存储为日期,然后指定here等属性
  • 如果不是,请确保在存储值时将start & end 的格式转换为“YYYY-MM-DD”

【讨论】:

  • 是的开始和结束存储为日期
猜你喜欢
  • 2016-08-05
  • 2017-07-06
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 2022-10-13
  • 1970-01-01
相关资源
最近更新 更多