PHP - Future Date In X Days

From Global Programming Syntax

Jump to: navigation, search

In rare cases you might want to display what the date will be in lets say 193 days 2 months. To do this, a function has been created so that you can just enter that data and it will be displayed. So first place the following code at the top of your php file.

function future_date($day,$month,$year) {
$str='+';
if ($year>1) {
$str.=$year.' years';
} else if ($year===1) {
$str.=$year.' year';
}
if ($month>1) {
$str.=$month.' months';
} else if ($month===1) {
$str.=$month.' month';
}
if ($day>1) {
$str.=$day.' days';
} else if ($day===1) {
$str.=$day.' day';
}
if ($str=='+') {
return date('j-n-Y');
} else {
return date('j-n-Y',strtotime($str));
}
}

Then to display what the date will be in 193 days 2 months, just use the following:

//(days/months/years)
echo future_date(193,2,0);

So after calling that function, it will display the date in the format day-month-year. Its that simple.

Personal tools
languages
page stats
Toolbox