Page 1 of 1

Counting dates?

Posted: Sat Dec 23, 2006 12:40 am
by cturner
I am wanting to calculate the difference between two dates then display that many textboxes. Can someone please tell me how I can achieve this? Thanks in advance.

This code is what I am working with:

Code: Select all

$now = time();
        $lastDay = date("t", $now);
        $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
        $thisMonth = date("F");
        $thisYear = date("Y");
        
        print "<select name=\"selectMonth\" id=\"selectMonth\">";
        foreach($months as $v)
        {
            $selStr = $v == $thisMonth ? " selected=\"selected\"" : "";
            print "<option value=\"$v\"$selStr>$v</option>";
        }
        print "</select>";

        print "<select name=\"selectYear\" id=\"selectYear\">";
        for($i=$thisYear; $i<$thisYear+30; $i++)
        {
            $selStr = $i == $thisYear ? " selected=\"selected\"" : "";
            print "<option value=\"$i\"$selStr>$i</option>";
        }
        print "</select>";

Posted: Sat Dec 23, 2006 3:30 am
by timvw
You could do it in a loop...

Code: Select all

$current = $begin;
while ($current < $ start) { 
 // create item
 // move to next day, since you're working with unixtimestamps, $current += (60 * 60 * 24)
}

Posted: Sat Dec 23, 2006 6:50 am
by Ollie Saunders
Off the top of my head something like this...

Code: Select all

$timeA = mktime(/* parameters here */);
$timeB = mktime(/* parameters here */);
$secondsInADay = 24 * 60 * 60;
$days = ($timeA - $timeB) / $secondsInADay;
for ($i = 0; $i < $days; ++$i) {
   echo 'Something ' . $i . '<br />';
}