Re: if(date("Y-m-d") >

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 2005-01-10 at 12:08, John Taylor-Johnston wrote:
> Hi,
> I would like some help to improve this script. I'm a teacher with a schedule of 17 weeks.
> Instead of using if(date("Y-m-d") >= $week3)  I would like to do a "for i = 1 to 17" and if the current date date("Y-m-d") = week[i] I would like to echo "This is week $week[i]";
> 
> Can someone show me how please?
> 
> 
> <?php
> #old code:
> $week1 = "2005-01-17";
> $week2 = "2005-01-24";
> ...
> $week17 = "2005-05-09;
> 
> if(date("Y-m-d") >= $week3)
> {
> echo "this is week 3");
> }
> ?>
> 

(Mistakenly sent to the OP only and I could not stand having my code not
seen by all :)  tested in a shell not web page so YMMV.  Now as I look
at it, a valid date in the last week would not work since there was no
end defined.  I added a 18th date and modified the if to < next week
date.  This code should work with no assumptions that today is even in
the range of weeks defined.

You practically wrote it already.  Also you can use a for loop to load
the array of dates.
$m=1;
$d=11;
$y=2005;

//Load week array
$week= array();
for ($i=0; $i<= 17; $i++ ) {
    $week[$i] = date("Y-m-d",mktime(0, 0, 0, $m  , $d + (7*$i), $y));
}
$today=date("Y-m-d");
for ($i=0; $i<= 16; $i++ ){
    if ("$today" >=  "$week[$i]" and "$today" < $week[$i+1] ){
        echo "This is week ". ($i + 1) . " that began on $week[$i]\n";
        exit;
    }
}
echo "could not find what week $today is in\n";
echo "start date is $y-$m-$d\n";
echo "end date is $week[17]\n";


HTH

Bret

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux