Strange date increment problem

Strange date increment problem

am 08.11.2007 20:06:08 von Reg143

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
----------------------------------------------------


// ----------------------------------
// 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].'
';
}

// ------------------------------------------------------
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;

}
?>

Re: Strange date increment problem

am 08.11.2007 20:21:53 von zeldorblat

On Nov 8, 2:06 pm, Reg143 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
> ----------------------------------------------------
>
> >
> // ----------------------------------
> // 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].'
';
>
> }
>
> // ------------------------------------------------------
> 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) . '
';

Re: Strange date increment problem

am 08.11.2007 21:43:51 von Jerry Stuckle

Reg143 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
> ----------------------------------------------------
>
> >
> // ----------------------------------
> // 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].'
';
> }
>
> // ------------------------------------------------------
> 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;
>
> }
> ?>
>
>

Why go to all the trouble with your functions when PHP has them for you?

This does the same thing, using the built-in functions:

$date[0] = "2007-11-01";
$day[0] = date('l', strtotime($date[0]));

// $date[0] = ('11-01-2007 + 1 day');

for ($intX=1;$intX < 7;$intX++) {
$tmp = strtotime($date[$intX - 1] . ' + 1 day');
$date[$intX] = date('Y-m-d', $tmp);
$day[$intX] = date('l', $tmp);
}

for ($intX=0;$intX < 7;$intX++) {
echo $day[$intX].' '.$date[$intX].'
';
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Strange date increment problem

am 08.11.2007 22:00:45 von luiheidsgoeroe

On Thu, 08 Nov 2007 20:06:08 +0100, Reg143 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.

I've tested the unaltered script (well, I disabled error reporting to =

check because there's no timezone set): works OK here. If it doesn't for=
=

you, var_dump() your variables and see what happens.


Then again, this does the trick:
date_default_timezone_set('EST');
$date =3D array();
$date[] =3D strtotime('2007-11-01');
for ($intX=3D0;$intX < 7;$intX++) {
echo date('Y-m-d',$date[$intX]).' '.date('l',$date[$intX])."
\n";
$date[$intX+1] =3D strtotime('+ 1 day', $date[$intX]);
}
?>
-- =

Rik Wasmus