Re: Date (Year) .. adding..
| From: | Rob Hardowa | Date: | Thu, 24 May 2001 07:12:30 +0000 |
| Subject: | Re: Date (Year) .. adding.. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-54173@lists.php.net to get a copy of this message | ||
I like Tyler's solution. It is better to move the call to date() outside the
loop. You only need to get the current year once, and then add one onto it
each iteration. If you leave the call to date() inside the loop then it
calculates the current date each time and that uses CPU power and time.
Looking at it in list form, here is the difference:
Original Solution
-----------------
Set x to 0
while x is less than 20
calculate current year
add value of x to year
print year
increase x by 1
New solution
------------------
set x to 0
Get current year
while x is less than 20
print year
increase year by one
increment x by 1
It's much faster to not have to call a function when it's not required, or in
this case, where the value has already been computed....especially when the
function is in a loop. :)
On Tuesday 22 May 2001 09:30 pm, you wrote:
> <?
> $x=0;
> $year = date("Y");
> while($x < 20)
> {
> $year = $year+1;
> print($year . "\n");
> $x++;
> }
> ?>
>
> Try that.
>
> Tyler
>
> > -----Original Message-----
> > From: Jason Caldwell [mailto:jason@thinkingman.org]
> > Sent: Tuesday, May 22, 2001 11:20 PM
> > To: php-general@lists.php.net
> > Subject: [PHP] Date (Year) .. adding..
> >
> >
> > I'm trying to figure out how to add to the year:
> >
> > for($x=0; $x<20; $x++)
> > {
> > $year = date("Y" + $x);
> > print($year . "\n");
> > }
> >
> > I've tried several variations on the above and cannot get the year to
> > come out.
> >
> > Any suggestions?
> >
> > Thanks
> > Jason
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> > For additional commands, e-mail: php-general-help@lists.php.net
> > To contact the list administrators, e-mail: php-list-admin@lists.php.net