Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bksurik's avatar

Automatic activation and deactivation promotions by date and time

It is necessary to implement automatic activation and deactivation of promotions by date and time.

There is a table of events which has fields:

table events( id bigint unsigned, title varchar(255), status tinyint(1) default 0, starts_at timestamp, ends_at timestamp )

How to change status to active when date and time is equal to starts_at value ? How to change status to inactive when datetime is equal to ends _at value ? Thanks in advance.

0 likes
3 replies
tykus's avatar

You are relying unnecessarily on an extra piece of data (status) to determine the active state of an Event; you already represent this state with the dates starts_at and ends _at.

You can use an Eloquent Query Scope to fetch active Events:

public function scopeActive(Builder $builder)
{
    $builder->whereDate('starts_at', '<=', today())->whereDate('ends_at', '>=', today());
}

and you can represent the status on the model with an Accessor:

public function active(): Attribute
{
    return new Attribute(
        get: fn () => $this->starts_at <= today() && $this->ends_at >= today()
    );
}

The alternative is to create a scheduled task to check every record in the table, and update the status when the condition is satisfied. Again, IMHO this is unnecessary.

bksurik's avatar

@tykus Thanks for the answer. This is necessary for a running system, that is, it is necessary to modify the activation and deactivation functionality of the action by the status field.

tykus's avatar

@bksurik scheduled job in that case.

DB::transaction(function () {
	DB::table('events')->update(['status' => false]);

    DB::table('events')
        ->whereDate('starts_at', '<=', today())
        ->whereDate('ends_at', '>=', today())
        ->>update(['status' => true]);
});

Please or to participate in this conversation.