T oday I am going to tech you that how to add or subtract days from current date in php. I often use this code for getting my required date. So let’s start. Look at below code:-
|
<?php //Adds 7 days in current date $date1 = date('M d, Y', strtotime('+7 days') ) ."</br>"; echo $date1; //Subtracts 7 days from current date $date2 = date('M d, Y', strtotime('-7 days') ) ."</br>"; echo $date2; ?> |
Add you can see we used two functions
date() and
strtotime(), in date() function we have supplied format for date and in strtotime we have add 7 days and then echo it. Similarly we can subtract days from current date. In the above example we used current date but you can also used your custom date. See below code
|
<?php //Adds 7 days in custome date $custom_date = strtotime('2015-01-17'); $end_date = strtotime("+7 day", $custom_date); echo date('Y-m-d', $end_date) . "<br>"; //Subtracts 7 days from custome date $custom_date = strtotime('2015-01-17'); $end_date = strtotime("-7 day", $custom_date); echo date('Y-m-d', $end_date); ?> |
I think there is nothing complicated in above example. We just used custom date instead of current date. If you need some more help, please ask in comment. You can download source code from below.
Like this:
Like Loading...
Related
insert your data