Re: Problem in FOR loop

From: Jonathan Wright <mail(at)djnauk(dot)co(dot)uk>
To: Ramesh PAtel <ramesh(at)banas(dot)guj(dot)nic(dot)in>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Problem in FOR loop
Date: 2003-05-01 13:07:34
Message-ID: 20030501130733.GA12934@chef.djnauk.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2003-05-01 14:13:56 Re: Optimizing POSTGRESQL config
Previous Message Joanne Formoso 2003-05-01 13:02:38 Optimizing POSTGRESQL config