mardi 5 mai 2015

Simpler way for this query: If {current date} is a {weekly/fortnightly/monthly} repeat/multiple of {start_date}

We have a table for scheduled_jobs, this table includes the following two collumns:

Repeat Type: Weekly/Fortnightly/Monthly

Beginning Date: The date it will repeat from, i.e. if repeat type is monthly and beginning date is set to 2015-12-05, it will repeat on the fifth of each month.

The below query is a snippet of code that is currently being used:

<?php
    foreach(range(1,100) as $num)
    {
        $numtimestwo = $num * 2;

        $query = "SELECT * FROM scheduled_jobs WHERE deleted != TRUE AND ((DATE_ADD(DATE(beginning_date), INTERVAL $num MONTH) = CURDATE() AND repeat_type = 'monthly') OR (DATE_ADD(DATE(beginning_date), INTERVAL $numtimestwo WEEK) = CURDATE() AND repeat_type = 'fortnightly') OR (DATE_ADD(DATE(beginning_date), INTERVAL $num WEEK) = CURDATE() AND repeat_type = 'weekly'))";

        // If result is not empty, process the scheduled job
    }
?>

But I've noticed some problems, this will eventually stop working as beginning_date gets older and older, and it seems like there should be a better solution to this.

Aucun commentaire:

Enregistrer un commentaire