473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Strange date increment problem

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;

}
?>

Nov 8 '07 #1
3 3033
On Nov 8, 2:06 pm, Reg143 <Reg...@aol.comwrote:
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/>';

Nov 8 '07 #2
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
----------------------------------------------------

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

}
?>

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

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

Nov 8 '07 #3
On Thu, 08 Nov 2007 20:06:08 +0100, Reg143 <Re****@aol.comwrote:
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:
<?php
date_default_timezone_set('EST');
$date = array();
$date[] = strtotime('2007-11-01');
for ($intX=0;$intX < 7;$intX++) {
echo date('Y-m-d',$date[$intX]).' '.date('l',$date[$intX])."<br>\n";
$date[$intX+1] = strtotime('+ 1 day', $date[$intX]);
}
?>
--
Rik Wasmus
Nov 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Treetop | last post by:
I have a script for my church that we use for the weekly events. I currently have it as week of Feb 1, 2003 at the top, then list Sun - Sat below the date. I have been asked to put the date next...
2
by: James Niceguy | last post by:
Hi, I have written a simple program that does the following: The main program will spown MAX_THREADS number of threads, each of which will simply add to a global shared counter for MAX_COUNT...
7
by: John Baker | last post by:
HI: I have a date, and am trying to do something that SHOULD be very simple -- but finding a problem. Assume the date is 01/01/03. I wish to make the date 02/01/03, and then next month 03/01/03...
1
by: Ken | last post by:
I wrote a function to use in queries that takes a date and adds or subtracts a certain length time and then returns the new value. There are times when my function needs to return Null values. ...
17
by: jensen bredal | last post by:
Hello, i'm struggling with a somehow badly understood session scenario. I provide acces to my pages based on form authentication using Session cookies. Som of my pages are supposed to be...
3
by: Jim in Arizona | last post by:
I'm going insane! I don't know if it's just that the .net 2.0 framework is buggy or if it really is my code. This is pretty hard to explain since I can't even begin to nail down why this is...
0
by: venky | last post by:
Can anybody let me know is windows date time control with time format at hh:mm:ss , when i click to increment , by default it increments the hour value, but for some controls i want to increment...
6
by: gre1unix | last post by:
I am new to VBA Access. I create "mytable" with 2 columns from existing main table by giving start date and end date. Given below is mytable. First column: dt( date hr:min:sec format ) Second...
1
by: Victor | last post by:
Hi guys, I have a very strange problem with scriptmanager here. I want to load a js (which is embed in the project) but everytime i try to load that, it gives me error like Specified argument was...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.