Saturday, September 12, 2015

PHP add number of days into date

PHP supports various date types and its conversion.
The following php example demonstrate to add n number of days in the provided date.

For date format: yyyy-mm-dd

function addDate($date,$n)
  {
 $dateArray=explode('-',$date);
 $addDate = mktime(0,0,0,$dateArray[1],$dateArray[2]+$n,$dateArray[0]);
 return date("Y-m-d", $addDate);
  }
  
  Usage of the function is as follows:
 
  $n = 1;
  $date = "2014-06-01";
 
  echo addDate($date, $n);
 
  Ouput is: 2014-06-02
 
For date format: yyyy/mm/dd

function addDate($date,$n)
  {
 $dateArray=explode('/',$date);
 $addDate = mktime(0,0,0,$dateArray[1],$dateArray[2]+$n,$dateArray[0]);
 return date("Y-m-d", $addDate);
  }
 
  Usage of the function is as follows:
 
  $n = 1;
  $date = "2014/06/01";
 
  echo addDate($date, $n);
 
  Ouput is: 2014/06/02

No comments:

Post a Comment

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...