How to increase the number from 1 (day / year) in PHP?

I have a date stored in an array:

$this->lines['uDate']

The date format is not fixed. I can be changed as follows:

  define('DATETIME_FORMAT', 'y-m-d H:i');

How can I increase uDate with a certain number of days or years?


My question is related to this:
add a date for the month
However, in my case, the date format is in dynamic mode.
So can I do this?

$time= $this->lines['uDate'];
$time = date(DATETIME_FORMAT, strtotime("+1 day", $time));
$this->lines['uDate']= $time;
+4
source share
3 answers
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;
}

I found this in PHP help

+2
source

date_add ()

and consider changes such as:

define(DATETIME_FORMAT, 'y-m-d H:i');

$time = date(DATETIME_FORMAT, strtotime("+1 day", $time));
+4
source

You can use some simple calculations to do this if you have a timestamp.

$date = strtotime($this->lines['uDate']); //assuming it not a timestamp\
$date = $date + (60 * 60 * 24); //increase date by 1 day
echo  date('d-m-y', $date);
$date = $date + (60 * 60 * 24 * 365); //increase date by a year
echo  date('d-m-y', $date);

You can also use the mktime () method to do this: http://php.net/manual/en/function.mktime.php

+2
source

All Articles