Re: Incrementing dates

From: Date: Sun, 22 Apr 2001 07:14:41 +0000
Subject: Re: Incrementing dates
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-49724@lists.php.net to get a copy of this message
James, Yz wrote: >If anyone has any comments on this, I'd like to > hear > them (there's probably a simpler way around what I have done). Here's the > URL: > > http://www.yorkshire-zone.co.uk/date_increment.php > > And here's the code that powers it: > > <HTML> > <BODY> > > <? > > $date = date("2001-04-28"); > list($year, $month, $day) = explode("-", $date); > > $actual_date = mktime(0,0,0,$month,$day,$year); > > $days_to_add = 7; > > $x = 1; > > while($x <= $days_to_add) { > > $make_date = getdate($actual_date); > > echo "Day $x: $make_date[mday] $make_date[month], > $make_date[year]<BR><BR>"; > > $actual_date = $actual_date + (3600 * 24); > > $x++; > > } > > ?> > > </body> > </html> Thanks James! You started me off in the right direction. And ... with a few beers less in the brain (and a look at php.net and PHP Developers Cookbook) I finally found that mktime actually increments dates correctly. So, given that $month, $day and $year are valid and that we want to add 10 days to the output we can do: echo date ("Y-m-d", mktime (0,0,0,$month, $day, $year)) . "<br>"; for($i=1; $i<=9; $i++) { $day++; echo date ("Y-m-d", mktime (0,0,0,$month, $day, $year)) . "<br>"; } Cheers, Martin S.

« previous php.general (#49724) next »