How to add days to date using PHP

No comments
Whether, you are printing a future date or you are creating a report in which, you need to create a weekly report or a monthly report and you want to add n number of days in current date to create your desired report then in such conditions this blog is useful for you.

So lets start with the ways, we can add days into date by using PHP language. There couple of ways, first one is, If you are using PHP 5.3 or greter version then you can add days to date with DateInterval() function. And if you are using lower version than PHP 5.3 then you should use PHP inbuilt function date() and date() function also works in latest PHP versions.

PHP 5.3
$Date1 = '2019-04-10';
$date = new DateTime($Date1);
$date->add(new DateInterval('P1D')); // Here P1D means a period of 1 day
$Date2 = $date->format('Y-m-d');

and the second method is:
$Date = date('Y-m-d', strtotime("2019-04-10" . " + 1 day")); // Here +1 is used for adding 1 day into date. If you add -1 instead of +1 it will print 09-04-2019

Hope you will like this blog. Please let me know, your comments below.

No comments :

Post a Comment