Search code examples
laravelilluminate

difference between 2 dates - illuminate manager laravel


I make a tv program schedule and I need to sort dates that are more than 600 seconds apart

But it don't work ;(

Anyone know how to do it?

Many thanks in advance to those who will help me.

$dateMin = Carbon::now('Europe/Paris')
    ->endOfDay()
    ->addDay()
    ->addHours(-4)
    ->timestamp;
    
$dateMax = Carbon::now('Europe/Paris')
    ->endOfDay()
    ->addDay()
    ->timestamp;

$datas = Capsule::table('channels')
    ->select('channels.number',
        'channels.slug',
        'channels.display-name',
        'channels.icon',
        'programs.start',
        'programs.stop',
        'programs.title',
        'programs.img',
        'programs.thumbnail',
        'programs.desc'
    )
    ->where([
        ['start', '>', $dateMin],
        ['stop', '>', $dateMin],
        ['start', '<', $dateMax],
    ])
    ->whereRaw('stop - start > 601')
    ->leftJoin('programs', 'channels.slug', '=', 'programs.slug')
    ->orderBy('number')
    ->orderBy('start')
    ->get();

    return $datas->groupBy('display-name');

Solution

  • Without much context, the only thing I can think of is to try grouping the parameters in where clause

    $datas = Capsule::table('channels')
        ->select('channels.number',
            'channels.slug',
            'channels.display-name',
            'channels.icon',
            'programs.start',
            'programs.stop',
            'programs.title',
            'programs.img',
            'programs.thumbnail',
            'programs.desc'
        )
        ->where(function($query) use($dateMin,$dateMax) {
            $query->where([
                ['start', '>', $dateMin],
                ['stop', '>', $dateMin],
                ['start', '<', $dateMax],
            ])
            ->whereRaw('stop - start > 601')
        })
        ->leftJoin('programs', 'channels.slug', '=', 'programs.slug')
        ->orderBy('number')
        ->orderBy('start')
        ->get();
    

    If this doesn't work, try dumping the query sql for both, without parameter grouping (code in your question) and with parameter grouping (code above in my answer) and see the sql statement generated. You can get the query sql using toSql() in place of get().