This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

wmsomnium9780
wmsomnium9780

Hi, don't judge strictly.

This function calculates the occurrence in time interval. I have no mathematical education

Please see this function and give advice. Correct if I approach?

Inputs

[{
  "id": 12,
  "weekday": "mon",
  "begin": "09:00:00",
  "end": "01:00:00",
  "is_active": 1,
  "sort_order": 100,
  "delivery_id": 10,
  "created_at": "2016-03-23 22:05:05",
  "updated_at": "2016-03-23 22:05:05"
 },
 {
  "id": 13,
  "weekday": "tue",
  "begin": "09:00:00",
  "end": "01:00:00",
  "is_active": 1,
  "sort_order": 200,
  "delivery_id": 10,
  "created_at": "2016-03-23 22:05:05",
  "updated_at": "2016-03-23 22:05:05"
 },
 {
  "id": 14,
  "weekday": "wed",
  "begin": "09:00:00",
  "end": "01:00:00",
  "is_active": 1,
  "sort_order": 300,
  "delivery_id": 10,
  "created_at": "2016-03-23 22:05:05",
  "updated_at": "2016-03-24 00:21:05"
 },
 {
  "id": 15,
  "weekday": "thu",
  "begin": "09:00:00",
  "end": "01:00:00",
  "is_active": 1,
  "sort_order": 400,
  "delivery_id": 10,
  "created_at": "2016-03-23 22:05:05",
  "updated_at": "2016-03-23 22:05:05"
 },
 {
  "id": 16,
  "weekday": "fri",
  "begin": "09:00:00",
  "end": "01:00:00",
  "is_active": 1,
  "sort_order": 500,
  "delivery_id": 10,
  "created_at": "2016-03-23 22:05:05",
  "updated_at": "2016-03-23 22:05:05"
 },
 {
  "id": 17,
  "weekday": "sat",
  "begin": "09:00:00",
  "end": "01:00:00",
  "is_active": 1,
  "sort_order": 600,
  "delivery_id": 10,
  "created_at": "2016-03-23 22:05:05",
  "updated_at": "2016-03-23 22:05:05"
 },
 {
  "id": 18,
  "weekday": "sun",
  "begin": "09:00:00",
  "end": "01:00:00",
  "is_active": 1,
  "sort_order": 700,
  "delivery_id": 10,
  "created_at": "2016-03-23 22:05:05",
  "updated_at": "2016-03-23 22:05:05"
 }
]

Function listing

public function scopeIsOpen($query)
    {
        $now = Carbon::now();
        $is_open = false;
        $diff = 0;
        $interval = -1;
        $begin = 0;
        $end = 0;

        $yesterdayWeekday = Carbon::now()->addDay(-1)->format('D');
        $todayWeekday = Carbon::now()->format('D');
        $tommorowWeekday = Carbon::now()->addDay(1)->format('D');

        $q = clone $query;
        $yesterdayWorkday = $q->isActive()->whereWeekday($yesterdayWeekday)->first();
        $q = clone $query;
        $todayWorkday = $q->isActive()->whereWeekday($todayWeekday)->first();
        $q = clone $query;
        $tommorowWorkday = $q->isActive()->whereWeekday($tommorowWeekday)->first();

        $yesterday = Carbon::now()->addDay(-1);
        $today = Carbon::now();
        $tommorow = Carbon::now()->addDay(+1);

        $begin_yesterday = $yesterday->copy()->setTimeFromTimeString($yesterdayWorkday->begin);
        $end_yesterday = $yesterday->copy()->setTimeFromTimeString($yesterdayWorkday->end);

        if ($end_yesterday <= $begin_yesterday) {
            $end_yesterday = $end_yesterday->addDay(+1);
        }

        $begin_today = $today->copy()->setTimeFromTimeString($todayWorkday->begin);
        $end_today = $today->copy()->setTimeFromTimeString($todayWorkday->end);

        if ($end_today <= $begin_today) {
            $end_today = $end_today->addDay(+1);
        }

        $begin_tommorow = $tommorow->copy()->setTimeFromTimeString($tommorowWorkday->begin);
        $end_tommorow = $tommorow->copy()->setTimeFromTimeString($tommorowWorkday->end);

        if ($end_tommorow <= $begin_tommorow) {
            $end_tommorow = $end_tommorow->addDay(+1);
        }

        if ($begin_yesterday < $now && $now < $end_yesterday) {
            $is_open = true;
            $interval = 0;
            $begin = $begin_yesterday;
            $end = $end_yesterday;
            $diff = $end_yesterday->diffInHours($now);
        } elseif($end_yesterday < $now && $now < $begin_today) {
            $is_open = false;
            $interval = 1;
            $begin = $end_yesterday;
            $end = $begin_today;
            $diff = $begin_today->diffInHours($now);
        }

        if ($begin_today < $now && $now < $end_today) {
            $is_open = true;
            $interval = 2;
            $begin = $begin_today;
            $end = $end_today;
            $diff = $end_today->diff($now);
        } elseif($end_today < $now && $now < $begin_tommorow) {
            $is_open = false;
            $interval = 3;
            $begin = $end_today;
            $end = $begin_tommorow;
            $diff = $begin_tommorow->diffInHours($now);
        }

        if ($begin_tommorow < $now && $now < $end_tommorow) {
            $is_open = true;
            $interval = 4;
            $begin = $begin_tommorow;
            $end = $end_tommorow;
            $diff = $end_tommorow->diffInHours($now);
        }

        return [
            'now' => $now,
            'diff' => $diff,
            'begin' => $begin,
            'end' => $end,
            'interval' => $interval,
            'is_open'=>$is_open
        ];
    }

Last updated

1-1 of 1

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.