downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

date_create_from_format> <checkdate
[edit] Last updated: Fri, 26 Apr 2013

view this page in

date_add

(PHP 5 >= 5.3.0)

date_addAlias of DateTime::add()

Description

This function is an alias of: DateTime::add()



date_create_from_format> <checkdate
[edit] Last updated: Fri, 26 Apr 2013
 
add a note add a note User Contributed Notes date_add - [4 notes]
up
0
lanlife4real
3 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
0
Hyun Woo Shin
4 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;
}
?>
up
0
raph
4 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
-1
knyrii at gmail dot com
1 year ago
A function I use for fixing date ranges. Can also be used to create a new date by subtracting or adding days, months, or years. The resulting date will always be a valid date.
<?php
/**
 * Fixes the date so that it is valid. Can handle negative values. Great for creating date ranges.
 * NOTE: Changes the passed variables' values.
 * Ex. 2-29-2001 will become 3-1-2001
 * 15-6-2011 will become 3-6-2012
 * 3-0-2001 will become 2-28-2001
 * -10-29-2002 will become 3-1-2001
 * @param integer $month 0 values decrement a year and is set to 12.
 * @param integer $day 0 values decrement a month and is set to the last day of that month.
 * @param integer $year
 * @param boolean $unix Optional. Default is true. Use UNIX date format
 * @return string The resulting string
 */
function date_fix_date(&$month,&$day,&$year,$unix=true){
    if(
$month>12){
        while (
$month>12){
           
$month-=12;//subtract a $year
           
$year++;//add a $year
       
}
    } else if (
$month<1){
        while (
$month<1){
           
$month +=12;//add a $year
           
$year--;//subtract a $year
       
}
    }
    if (
$day>31){
        while (
$day>31){
            if (
$month==2){
                if (
is_leap_year($year)){//subtract a $month
                   
$day-=29;
                } else{
                   
$day-=28;
                }
               
$month++;//add a $month
           
} else if (date_hasThirtyOneDays($month)){
               
$day-=31;
               
$month++;
            } else{
               
$day-=30;
               
$month++;
            }
        }
//end while
       
while ($month>12){ //recheck $months
           
$month-=12;//subtract a $year
           
$year++;//add a $year
       
}
    } else if (
$day<1){
        while (
$day<1){
           
$month--;//subtract a $month
           
if ($month==2){
                if (
is_leap_year($year)){//add a $month
                   
$day+=29;
                }else{
                   
$day+=28;
                }
            } else if (
date_hasThirtyOneDays($month)){
               
$day+=31;
            } else{
               
$day+=30;
            }
        }
//end while
       
while ($month<1){//recheck $months
           
$month+=12;//add a $year
           
$year--;//subtract a $year
       
}
    } else if (
$month==2){
        if (
is_leap_year($year)&&$day>29){
           
$day-=29;
           
$month++;
        } else if(
$day>28){
           
$day-=28;
           
$month++;
        }
    } else if (!
date_hasThirtyOneDays($month)&&$day>30){
       
$day-=30;
       
$month++;
    }
    if (
$year<1900) $year=1900;
    if (
$unix){
        return
"$year-$month-$day";
    } else{
        return
"$month-$day-$year";
    }
}
/**
 * Checks to see if the month has 31 days.
 * @param integer $month
 * @return boolean True if the month has 31 days
 */
function date_hasThirtyOneDays($month){
   
//1234567 89012:1357 802
    //JfMaMjJ AsOnD:JMMJ AOD
   
if ($month<8)
        return
$month%2==1;
    else
        return
$month%2==0;
}
/**
 * Checks to see if the year is a leap year.
 * @param integer $year
 * @return boolean True if the year is a leap year
 */
function is_leap_year($year){
    return (
0 ==$year%4&&0!=$year%100 || 0 ==$year%400);
}
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites