Strange date increment problem

Strange date increment problem

am 08.11.2007 20:07:10 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:39:47 von Shion

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.
> // 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?

Did run your script

Thursday 2007-11-01
Friday 2007-11-02
Saturday 2007-11-03 />Sunday 2007-11-04
Monday 2007-11-05
Tuesday 2007-11-06 />Wednesday 2007-11-07


Seems there may be something wrong with your FPU ;)

--

//Aho

Re: Strange date increment problem

am 08.11.2007 20:57:27 von Steve

"Reg143" wrote in message
news:Xns99E28F9944564Reg143aolcom@199.45.49.11...
> 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?
> // ------------------------------------

why are you doing all that?

$date = strtotime('2007-11-04');
$dates = array();
for ($i = 0, $i < 7; $i++)
{
$dates[$i] = getdate(strtotime('+' . $i . ' day', $date));
}
foreach ($dates as $date)
{
$date[0] = date('Y-m-d', $date[0]);
echo '

' .
date('Y-m-d', $date[0]) .
' is on a ' .
$date['weekday'] .
'
';
}

in your example, you assume that every server you run your code on begins on
a monday - a zero index. that is entirely configurable. zero may very well
be a sunday on some systems. getdate() takes that configuration into
account. no need to build your own functions for any of that...just learn
what's at your desposal. checking the manual now and again surely helps. :)

Re: Strange date increment problem

am 09.11.2007 04:10:24 von Steve

"Steve" wrote in message
news:9SJYi.81$e61.27@newsfe06.lga...
>
> "Reg143" wrote in message
> news:Xns99E28F9944564Reg143aolcom@199.45.49.11...
>> 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?
>> // ------------------------------------

sorry, saw an obvious mistake...this fixes it:

$date = strtotime('2007-11-04');
$dates = array();
for ($i = 0; $i < 7; $i++)
{
$dates[] = getdate(strtotime('+' . $i . ' day', $date));
}
foreach ($dates as $date)
{
$date[0] = date('Y-m-d', $date[0]);
echo '

' .
$date[0] .
' is on a ' .
$date['weekday'] .
'
';
}