PHP 5.4.33 Released

date_add

(PHP 5 >= 5.3.0)

date_addAlias de DateTime::add()

DescripciĆ³n

Esta funciĆ³n es un alias de: DateTime::add()

add a note add a note

User Contributed Notes 5 notes

up
-4
detain at interserver dot net
1 year ago
<?php
function mysql_date_add($now = null, $adjustment )
{     
       
// normal mysql format is:   date_add(now(), INTERVAL 1 MONTH)
        // its close to the strtotime() format, but we need to make a few adjustments
        // first we lowercase everything, not sure if this is needed but it seems
        // to be both mysql conventions to be capitalized and php to lowercase this, so
        // i follow suit.
       
$adjustment = strtolower($adjustment);
       
// next we want to get rid of the INTERVAL part, as neither it nor a corrisponding
        // keyword used in the strtotime() function.   remmeber its lowercase now.
       
$adjustment = str_replace('interval', '', $adjustment);
       
// now the adjustment is suppsoed to have a + or - next to it to indicate direction
        // since strtotime() can be used to go both ways.  We want to tack this one, but first
        // strip any white space off the begining of the $adjustment so we dont wind up with like
        //  +     1         when we need    +1
       
$adjustment = '+' . trim($adjustment);
       
// we should now be left with something like '+1 month'  which is valid strtotime() syntax!
        // next we need to handle the $now, normally people would pass now() if they want the current
        // time or a datetime/timestamp.    We will need to account for this as well, we also
        // want to make use of having a default to now() type of behavior.    we want to also
        // trim and lowercase what they send us just to make it easier to compair to
       
if (is_null($now) || strtolower(trim($now)) == 'now()')
        {
               
// defaulted to or requested a the current time
               
$now = time();
        }
        else
        {
               
// here we are splitting out each part of the mysql timestamp , and storing it in the $parts array
                
preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $now, $parts);
               
// now we use each of the parts to generate a timestamp from it
               
$now = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]);
        }
       
// now we finally call strtotime() with the properly formatted text and get the date/time
        // calculates done.  I specify its returned as an integer to make things play more nicely
        // with eachother in case the conversion fails.
       
$timestamp = (integer)strtotime($adjustment, $now);
       
// finally we have the timestamp of the adjusted date nowe we just convert it back to the mysql
        // format and send it back to them.
       
return date('Y-m-d H:i:s', $timestamp);
}
?>
up
-5
antony at harrisretail dot co dot uk
5 months ago
When using DateTime::add() be careful that additions over Summertime changes will not always produce the expected results.  For instance, adding a day (interval = P1D) is probably expected to keep the same time when added to a date even over a summertime change. But adding 24 hours (interval = PT24H) does not seem to take into effect the time change.

When then checking the time difference after adding 24 hours after the clocks went forward, the time difference is only 23 hours.

<?php
date_default_timezone_set
('Europe/London');

$diff1Day = new DateInterval('P1D');
$diff24Hours = new DateInterval('PT24H');
$diff1440Minutes = new DateInterval('PT1440M');

// Clocks changed at 2014-03-30 02:00:00

$d0 = new DateTime('2014-03-29 08:00:00');

$d1 = new DateTime('2014-03-29 08:00:00');
// Add 1 day - expect time to remain at 08:00
$d1->add($diff1Day);
print_r($d1);

$d2 = new DateTime('2014-03-29 08:00:00');
// Add 24 hours - expect time to be 09:00
$d2->add($diff24Hours);
print_r($d2);

$seconds = $d1->getTimestamp() - $d0->getTimestamp();
echo
"Difference in Hours: " . $seconds / (60 * 60) . "\n";
up
-11
raph
6 years ago
A little function to add 2 time lenghts. Enjoy !

<?php
function AddPlayTime ($oldPlayTime, $PlayTimeToAdd) {

   
$pieces = split(':', $oldPlayTime);
   
$hours=$pieces[0];
   
$hours=str_replace("00","12",$hours);
   
$minutes=$pieces[1];
   
$seconds=$pieces[2];
   
$oldPlayTime=$hours.":".$minutes.":".$seconds;

   
$pieces = split(':', $PlayTimeToAdd);
   
$hours=$pieces[0];
   
$hours=str_replace("00","12",$hours);
   
$minutes=$pieces[1];
   
$seconds=$pieces[2];
   
   
$str = $str.$minutes." minute ".$seconds." second" ;
   
$str = "01/01/2000 ".$oldPlayTime." am + ".$hours." hour ".$minutes." minute ".$seconds." second" ;
   
   
// Avant PHP 5.1.0, vous devez comparer avec  -1, au lieu de false
   
if (($timestamp = strtotime($str)) === false) {
        return
false;
    } else {
       
$sum=date('h:i:s', $timestamp);
       
$pieces = split(':', $sum);
       
$hours=$pieces[0];
       
$hours=str_replace("12","00",$hours);
       
$minutes=$pieces[1];
       
$seconds=$pieces[2];
       
$sum=$hours.":".$minutes.":".$seconds;
       
        return
$sum;
       
    }
}

$firstTime="00:03:12";
$secondTime="02:04:34";

$sum=AddPlayTime($firstTime,$secondTime);
if (
$sum!=false) {
    echo
$firstTime." + ".$secondTime." === ".$sum;
}
else {
    echo
"failed";
}
?>
up
-13
lanlife4real
4 years ago
This function allows the addition of day(s),month(s),year(s) to the original date while still preserving the Hours, minutes and seconds
You can also modify to add to hours, miuntes and even seconds.

<?php
function add_date($givendate,$day=0,$mth=0,$yr=0) {
     
$cd = strtotime($givendate);
     
$newdate = date('Y-m-d h:i:s', mktime(date('h',$cd),
   
date('i',$cd), date('s',$cd), date('m',$cd)+$mth,
   
date('d',$cd)+$day, date('Y',$cd)+$yr));
      return
$newdate;
              }

?>
up
-12
Hyun Woo Shin
5 years ago
Just add month(s) on the orginal date.

<?php
function add_date($orgDate,$mth){
 
$cd = strtotime($orgDate);
 
$retDAY = date('Y-m-d', mktime(0,0,0,date('m',$cd)+$mth,date('d',$cd),date('Y',$cd)));
  return
$retDAY;
}
?>
To Top