You are here: Re: Strange date increment problem « PHP Programming Language « IT news, forums, messages
Re: Strange date increment problem

Posted by ZeldorBlat on 11/08/07 19:21

On Nov 8, 2:06 pm, Reg143 <Reg...@aol.com> wrote:
> Hi all,
>
> The code below loops from a starting date, incrementing the date and
> displaying date and day-of-week. Everything is fine until 2007-11-04 is
> reached. Any help would be appreciated.
>
> Thanks in advance.
>
> ----------------------------------------------------
> Dates.php
> ----------------------------------------------------
>
> <?php
>
> // ----------------------------------
> // This code works until the date hits 2007-11-04, it
> // never gets past 11/4. Run it starting with the
> // different dates below. What gives?
> // ------------------------------------
>
> $date[0] = "2007-11-01";
> //$date[0] = "2007-11-05";
> //$date[0] = "2007-12-31";
>
> $day[0] = DayOfWeek($date[0]);
>
> for ($intX=1;$intX < 7;$intX++) {
> $date[$intX] = DateAdd('d', $date[$intX - 1], 1);
> $day[$intX] = DayOfWeek($date[$intX]);
>
> }
>
> for ($intX=0;$intX < 7;$intX++) {
> echo $day[$intX].' '.$date[$intX].'<br />';
>
> }
>
> // ------------------------------------------------------
> function DayOfWeek($strDate) {
> $timestamp = mktime(0,0,0,substr($strDate,5,2),substr
> ($strDate,8,2),substr($strDate,0,4));
> $Dateinfo = getdate($timestamp);
> switch ($Dateinfo['wday']) {
> case 0:
> $ret = "Sunday";
> break;
> case 1:
> $ret = "Monday";
> break;
> case 2:
> $ret = "Tuesday";
> break;
> case 3:
> $ret = "Wednesday";
> break;
> case 4:
> $ret = "Thursday";
> break;
> case 5:
> $ret = "Friday";
> break;
> case 6:
> $ret = "Saturday";
> break;
> }
> return $ret;
>
> }
>
> // ------------------------------------------------------
> function DateAdd($interval, $strDate, $intNum) {
> // $strDate is in 'YYYY-MM-DD' format.
> // Convert to timestamp,
> // calculate new timestamp,
> // convert back to "YYYY-MM-DD"
>
> $date1 = mktime(0,0,0,substr($strDate,5,2),substr
> ($strDate,8,2),substr($strDate,0,4));
>
> switch ($interval) {
> case 'w':
> $date1 = $date1 + ($intNum * 604800);
> break;
> case 'd':
> $date1 = $date1 + ($intNum * 86400);
> break;
> case 'h':
> $date1 =$date1 + ($intNum * 3600);
> break;
> case 'n':
> $date1 = $date1 + ($intNum * 60);
> break;
> case 's':
> $date1 = $intNum;
> break;
> }
> $ret = date("Y-m-d",$date1);
> return $ret;
>
> }
>
> ?>

Maybe because of DST? I don't really want to try and figure out
exactly why this code doesn't work, so instead let me suggest
something a bit simpler that does what you're trying to do:

$startDate = strtotime('2007-11-01');
$endDate = strtotime('+7 days', $startDate);

for($i = $startDate; $i < $endDate; $i = strtotime('+1 day', $i))
echo date('l Y-m-d', $i) . '<br/>';

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация