Nov 11 2009

PHP Day add function

Category: PhpGiulio Pons @ 3:06 pm

How to add 2 days to a date in PHP?
There are many ways to add days to a string date. The best function is the following one:

function dayadd($days,$date=null , $format="d/m/Y"){
	return date($format,strtotime($days." days",strtotime( $date ? $date : date($format) )));
}

This function let you decide the date to which add the days, the output format and days to add. To use the function look these examples:

echo dayadd(2);  // 2 days after today
echo dayadd(-2,null,"Y-m-d");  // 2 days before today with given format
echo dayadd(3,"12/31/2009");  // 3 days after given date
echo dayadd(3,"12/31/2009","d-m-Y");  // 3 days after given date with given format
Share

Related posts:

  1. PHP bot to grab meteo information from Google
  2. PHP how to calculate age from date of birth
  3. PHP find previous monday from a date
  4. PHP function to fix collation on database fields of MySQL
  5. get MySpace events with a PHP function

Tags: , , ,

2 Responses to “PHP Day add function”

  1. JasonDavis says:

    Sorry I am not sure what kind of time can I pass into this? I store times as UTC time in a regualr integer filed in mysql, I am thinking my utc time stamp will not work?

  2. admin says:

    I use to work with DATE fields in my tables with YYYY-MM-DD format, but this function should work also with other formats (d-m-Y or m-d-Y), I’ve not tested every combination. You should add characters “-” to your integer date and convert it to a string to use this function.

Leave a Reply