Counting dates?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Counting dates?

Post 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>";
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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 />';
}
Post Reply