PHP Day add function

How to add 2 days to a date in PHP? There are many ways to add days to a string…

November 11, 2009

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

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “PHP Day add function”

2 thoughts

  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.

Comments are closed

Recommended

PHP how to calculate age from date of birth

This is a very simple script that starts from a string date in format yyyy-mm-dd and return the age. To…

February 10, 2010

Decimal Degrees conversion and distance of two points on google map

Those two functions are usefull when you’re making Google Maps applications: When you show the coordinates of a point, it’s…

December 26, 2009

Parse a float number in javascript

This small code parse a javascript float number to a string. It converts the number to a string and let…

November 9, 2009

A new avatar generator for WordPress with get_avatar and Flathash

Brand new avatars for WordPress Gravatar service

April 7, 2017

3 ways to add author’s image in WordPress

Add author's image in WordPress without a plugin

April 5, 2017

Orari trenord, corri solo quando ce n’è bisogno

Hai presente quando corri in stazione per prendere il treno al volo e, quando arrivi, ti accorgi che il treno ha 12 minuti di ritardo?

March 30, 2017