Jump to content

[SOLVED] Finding all the Mondays


soycharliente

Recommended Posts

I found this code online.

 

<?php
function findDays($startDate, $endDate, $days)
{
    $endDate = strtotime($endDate);
    $days = explode(',', $days);
    $dates = array();
    foreach ($days as $day)
    {
        $newDate = $startDate;
        switch ($day)
        {
            case 'Su': $day = 'Sun'; break;
            case 'M': $day = 'Mon'; break;
            case 'T': $day = 'Tue'; break;
            case 'W': $day = 'Wed'; break;
            case 'Th': $day = 'Thur'; break;
            case 'F'; $day = 'Fri'; break;
            case 'S': $day = 'Sat'; break;
        }
        while (($date = strtotime($newDate)) <= $endDate)
        {
            $dates[] = date("Y-m-d", $date) . "\n";
            $newDate = date("Y-m-d", $date) . " next " . $day;
        }
    }

    sort(array_unique($dates));
    return $dates;
}
?>

 

Obviously this code is very inefficient. It runs through every day.

 

Can someone suggest a better way to find all the dates (the ones I want to check against all reside in a database) that are a specific day of the week? I don't want code, but some steps to follow. I can't even begin to think about how to attack this. If this doesn't work out, I'm toying with the idea of just adding a db field that stores the day of the week.

Link to comment
https://forums.phpfreaks.com/topic/91703-solved-finding-all-the-mondays/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.