PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > Strange date increment problem
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Strange date increment problem

Réponse
 
LinkBack Outils de la discussion
Vieux 08/11/2007, 19h07   #1
Reg143
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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;

}
?>

  Réponse avec citation
Vieux 08/11/2007, 19h39   #2
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Strange date increment problem

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 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<br />Friday 2007-11-02<br />Saturday 2007-11-03<br
/>Sunday 2007-11-04<br />Monday 2007-11-05<br />Tuesday 2007-11-06<br
/>Wednesday 2007-11-07<br />

Seems there may be something wrong with your FPU

--

//Aho
  Réponse avec citation
Vieux 08/11/2007, 19h57   #3
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Strange date increment problem


"Reg143" <Reg143@aol.com> 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 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?
> // ------------------------------------


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 '<pre>' .
date('Y-m-d', $date[0]) .
' is on a ' .
$date['weekday'] .
'</pre>';
}

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


  Réponse avec citation
Vieux 09/11/2007, 03h10   #4
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Strange date increment problem


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


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 '<pre>' .
$date[0] .
' is on a ' .
$date['weekday'] .
'</pre>';
}


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h49.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11583 seconds with 12 queries