OneSeventeen
2 years ago

Calculate if model is active based on start and end dates

Posted 2 years ago by OneSeventeen

I have begin_at and end_at columns in a "surveys" table and am constantly adding code like this:

if( ($survey->begin_at == null || strtotime($survey->begin_at) < time())
    && ($survey->end_at == null || strtotime($survey->end_at) > time())) {
    //do stuff since the survey is active
}

I'd love to just add that in a method on my survey model so I can change that to:

if( $survey->active() ) {
 //do stuff since the survey is active
}

Especially considering I could check that in views as well.

Any thoughts on the best practice for accomplishing this? Or should I just keep repeating the more drawn out logic?

Please sign in or create an account to participate in this conversation.