Thread: Problem in FOR loop

Problem in FOR loop

From
"Ramesh PAtel "
Date:
Hi All

please give one  help

I want to us date variable in for loop Condition. in PHP

But it not working Properly.
$dt='2003-04-22' ;
$to='2003-04-30';

FOR ( $dt=$dt; $dt > $to ;  )
{
print "XYz";
}

Please Help me

and how to file date diff in Day please Help

Ramesh Patel


Re: Problem in FOR loop

From
Jonathan Wright
Date:
Ramesh PAtel  thus noted:
> Hi All
>
> please give one  help
>
> I want to us date variable in for loop Condition. in PHP
>
> But it not working Properly.
> $dt='2003-04-22' ;
> $to='2003-04-30';
>
> FOR ( $dt=$dt; $dt > $to ;  )
> {
> print "XYz";
> }
>
> Please Help me
>
> and how to file date diff in Day please Help

That won't work as there is nothing to increment the date within the for
loop:

for (<set variable>;<exit condition>;<condition modifier>);

You would need some that would take the date and increment it. One
output would be to use the mktime function to convert the date into
timestamp and then use that for the comparison

--8<--------------

function from_date($date) {
  list($year, $month, $date) = explode("-", $date) {
  return mktime(0, 0, 0, $month, $day, $year);
}

function to_date($date) {
  return date("Y-m-d", $date);
}

$dt='2003-04-22';
$to='2003-04-30';

for ( ; from_date($dt) > from_date($to); $dt = to_date(from_date($dt)+(60*60*24))) {
  print "XYz";
}

--8<--------------

I'm not sure if PHP can handle dates like you want, but that's one way
around the problem....

--
jonathan wright       mail at djnauk.co.uk | www.djnauk.co.uk
--
life has no meaning unless we can enjoy what we've been given