FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


A loop that increment a given date





pollux1er
Hi to everybody. I have a matter on making a function that return the following date. I mean, I want to increment a given for the purpose of my application. But I don't know how to build a loop that increment a given date. (In PHP Language)
Thanks to help me.
Peterssidan
If your date is stored as a unix timestamp you can add 86400 to it and add one day. (86400 seconds == 24 hours)
pollux1er
Peterssidan wrote:
If your date is stored as a unix timestamp you can add 86400 to it and add one day. (86400 seconds == 24 hours)


No. I want to write something like this :

Code:
function IncrementDate($date)


This function should return the date of the following day!
Peterssidan
And what format is $date in?
jmraker
Code:
<?php

define('INCREMENT_DAYS', 1);
define('INCREMENT_HOUR', 0);
define('INCREMENT_MIN', 0);
define('INCREMENT_SEC', 0);

function IncrementDate($date, $days=INCREMENT_DAYS, $hour=INCREMENT_HOUR, $min=INCREMENT_MIN, $sec=INCREMENT_SEC){
   if(is_string($date))
      $date = strtotime($date);
   return $date + ($days * 86400) + ($hour * 3600) + ($min * 60) + $sec;
}

$date = time();
echo strftime("%m/%d/%Y %I:%M:%S%p \n", $date);
echo strftime("%m/%d/%Y %I:%M:%S%p \n", IncrementDate($date));   // Use the defaults
echo strftime("%m/%d/%Y %I:%M:%S%p \n", IncrementDate("Jul 4 1988", 7));   // 7 days after Jul 4th
echo strftime("%m/%d/%Y %I:%M:%S%p \n", IncrementDate($date, 0, 1));   // 1 hour from now
echo strftime("%m/%d/%Y %I:%M:%S%p \n", IncrementDate($date, 0, 2, 2)); // 2 hours 2 minutes from now
echo strftime("%m/%d/%Y %I:%M:%S%p \n", IncrementDate($date, 0, 2, 2, 2)); // " " and 2 minutes from now
echo strftime("%m/%d/%Y %I:%M:%S%p \n", IncrementDate("1 year", 0));   // 1 year from now

?>


Where INCREMENT_DAYS is the default number of days to increment
and the date can be either a unix timestamp or a string like "Jul 4 1988" that strtotime understands, strtotime itself can increment dates which is why the "1 year" increment works.

Output
Code:
03/11/2009 12:14:50PM
03/12/2009 12:14:50PM
03/18/2009 12:14:50PM
07/11/1988 12:00:00AM
03/11/2009 02:16:50PM
03/11/2009 02:16:52PM
03/11/2010 12:14:50PM
pollux1er
I prefered from days to days. Thanks for the work you have already done. Me too since this morning I am trying to write something. It is not too easy.
pollux1er
Thanks to all for participating to this post.
I have written a function to get the next date, based on 2 functions. In case somebody needs it, this is it:

1. function to know if a year is bissextile:
Code:

function bissextile($annee)
   {
      if( (is_int($annee/4) && !is_int($annee/100)) || is_int($annee/400))
         {
            return TRUE;
         }
      else
         {
            return FALSE;
         }
   }


2. function to count the number of days:
Code:

function NbJr($mois, $annee)
      {
         $nbjr = 0;
         !is_int($mois) ? $mois = (int)$mois : $mois = $mois;
         !is_int($annee) ? $annee = (int)$annee : $annee = $annee;
         switch($mois)
            {
               case 1: $nbjr = 31; break;
               case 2: bissextile($annee) ? $nbjr = 29 : $nbjr = 28;  break;
               case 3: $nbjr = 31; break;
               case 4: $nbjr = 30; break;
               case 5: $nbjr = 31; break;
               case 6: $nbjr = 30; break;
               case 7: $nbjr = 31; break;
               case 8: $nbjr = 31; break;
               case 9: $nbjr = 30; break;
               case 10: $nbjr = 31; break;
               case 11: $nbjr = 30; break;
               case 12: $nbjr = 31; break;
            }
         return $nbjr;
      }


3. And the last function to increment with a given one :


Code:

function IncrementeDate($date)
      {
         $arr1 = explode("/", $date);
         for($i=0; $i<count($arr1); $i++)
            {
               $arr1[$i] = (int)$arr1[$i];
            }
         if($arr1[0]<28)
            {
               $arr1[0]++;
               $date = implode("/", $arr1);
            }
         elseif($arr1[0]==28)
            {
               if($arr1[1]==2)
                  {
                     if(bissextile($arr1[2]))
                        {
                           $arr1[0] = 29;
                           $date = implode("/", $arr1);
                        }
                     else
                        {
                           $arr1[1]++;
                           $arr1[0] = 1;
                           $date = implode("/", $arr1);
                        }
                  }
               else
                  {
                     $arr1[0] = 29;
                     $date = implode("/", $arr1);
                  }
            }
         elseif($arr1[0]==29)
            {
               if($arr1[1]==2)
                  {
                     $arr1[1]++;
                     $arr1[0] = 1;
                     $date = implode("/", $arr1);
                  }
               else
                  {
                     $arr1[0]++;
                     $date = implode("/", $arr1);
                  }
            }
         elseif($arr1[0]==30)
            {
               if(NbJr($arr1[1], $arr1[2])==30)
                  {
                     $arr1[1]++;
                     $arr1[0] = 1;
                     $date = implode("/", $arr1);
                  }
               elseif(NbJr($arr1[1], $arr1[2])==31)
                  {
                     $arr1[0]++;
                     $date = implode("/", $arr1);
                  }
               
            }
         elseif($arr1[0]==31)
            {
               if($arr1[1]==12)
                  {
                     $arr1[2]++;
                     $arr1[1] = 1;
                     $arr1[0] = 1;
                     $date = implode("/", $arr1);
                  }
               else
                  {
                     $arr1[1]++;
                     $arr1[0] = 1;
                     $date = implode("/", $arr1);
                  }
            }
         return $date;            
      }
kv
I don't think you should break your head that much. Just convert it to unix time stamp, add 86400 to it, convert back to date (or formatted date string). It should be that simple.
pollux1er
kv wrote:
I don't think you should break your head that much. Just convert it to unix time stamp, add 86400 to it, convert back to date (or formatted date string). It should be that simple.


I understand but I am not working on UNIX. Or let me know exactly what you are trying to tell me.
jmraker
All of the date math and date functions mentioned are part of PHP. They don't require unix.
pollux1er
jmraker wrote:
All of the date math and date functions mentioned are part of PHP. It don't require unix.

Ok. But is there any PHP function that gives you the date following a given date? Confused
rvec
Quote:
Just convert it to unix time stamp

http://nl2.php.net/manual/en/function.mktime.php
Quote:
add 86400 to it

Quote:
convert back to date (or formatted date string).

http://nl2.php.net/manual/en/function.date.php
pollux1er
rvec wrote:
Quote:
Just convert it to unix time stamp

http://nl2.php.net/manual/en/function.mktime.php
Quote:
add 86400 to it

Quote:
convert back to date (or formatted date string).

http://nl2.php.net/manual/en/function.date.php


I read it. A bit complicated. But I think I will make an effort to understand and write a better function shorter than the first one. Thanks at all rvec. Wink
Related topics
[PhP] News Posting Tutorial (code, actually ^^')
writing COM components in Perl
SAT- Longer Time Given for Wealthy Kids
Help!! Date question.. (SOLVED)
WHICH TAXES do you most enjoy paying and having others pay?
Simple menu written in C - Problem with loop
PSP GTA Liberty City Stories
The most powerful Yahoo! Bot
Character Coding problems in Zipped Files
Alternating between even and odd in a while loop
LOCK PLEASE
Date within a Timeframe
God is Perfect
Checkbox and Date filter with mysql and PHP
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.