1

Php have function for plus date or not ?

normally, if i want to plus date with 5 day i usually user this code.

<?PHP
$now = date("Y-m-d");
$now_explode = explode("-", $now);
$now_year = $now_explode[0];
$now_month = $now_explode[1];
$now_date = $now_explode[2];
$now_date = $now_date + 5;
$next_five_date = $now_year."-".$now_month."-".$now_date;
echo $next_five_date;
?>

But i have some issue eg: if $now = "2016-12-31";. When i run my code. Result will be 2016-12-36 Then i have to check month, check year for date in Feb month.

It's labyrinthine, so i want to know php have general function for plus date in to date ?

robert lovely
  • 55
  • 1
  • 6

1 Answers1

2

Here's one of many ways to do it:

$now = date("Y-m-d"); //how to get current date
$next_five_date = date('Y-m-d', strtotime('+5 days')); //how to get date +5 days
Chris
  • 57,622
  • 19
  • 111
  • 137
  • you could also add $now to the strtorime to make it better. `$next_five_date = date('Y-m-d', strtotime('+5 days', $now));` This way $now can be later and it will still work – Andreas May 05 '16 at 14:34