Page 1 of 1

Finding Dates between 2 Given Values

Posted: Wed Apr 22, 2009 10:11 am
by SheDesigns
I have a rather interesting Calendar/Event Application that has multiple uses, so I'm not able to change the DB structure for just one of them!

Some events are series, and with these events, I want to list all dates between the start date & end date that are equal to the days that the classes are being held. Enough confusing you, here's the way the DB is built (there are a ton of feilds, I'm only including a few!):

eventid (the unique id)
title (the event title)
type (the event type - workshop, series, etc)
days (the day the events occur [values could be: Sun, Mon, Tue, Wed, Thu, Fri, Sat])
firstday (first date the event is occuring)
lastday (last date the event is occuring)

Example Entry:
values('4', 'Live Your Best Life Now!', 'Series', 'Thu, Sat', '2009-05-07', '2009-06-05')

Code: Select all

if($type == "series")
{
echo "<ul>
            <li> $firstday";
 
// Here's where I need *HELP*. I need to list all the event days in between!!
 
echo "    <li> $lastday
       </ul>";
}
The catch is that this even isn't just weekly, it occurs twice a week. Also, some events will only be occuring once a week or once a month, so I cannot just write it to check every month.

Please help! 8O

Re: Finding Dates between 2 Given Values

Posted: Wed Apr 22, 2009 11:01 am
by requinix

Code: Select all

<?php
 
// variables here
$start = "2009-05-07";
$end = "2009-06-05";
$days = "Thu, Sat";
 
//
 
// don't like the format they're in so change them into something more appropriate
$start = strtotime($start);
$end = strtotime("$end +1 day"); // stop at the day after
$days = explode(", ", $days); // split into an array
// go from $start up to $end, adding a day each time
for ($date = $start; $date < $end; $date = strtotime("+1 day", $date)) {
        // if we like the day then print it
    if (in_array(date("D", $date), $days)) echo date("r", $date), "<br>\n";
}

Re: Finding Dates between 2 Given Values

Posted: Wed Apr 22, 2009 1:23 pm
by SheDesigns
Thank you! Thank you! Thank you! : )
That works perfectly.