Asked 1 years ago
9 Jun 2022
Views 466
debugger

debugger posted

how to subtract days to given date in php ?

how to subtract any numbers days to date in PHP ?

suppose , i want to subtract 5 days to current date or any date
sqltreat

sqltreat
answered Oct 8 '21 00:00

time()
By using time() function in PHP
time() return current timestamp and by subtract any number of days to it lead to time of before n(any number) days
for example :
subtract 1 days to the current date.

date('Y-m-d',time()-3600*24*1);


so here we are converting first given days to second by multiplying it with 3600*24 where (60 minute * 60 second)3600 second * 24 hour
in short 1 day is equal to 60 seconds per minute, 60 minutes per hour and 24 hours per day: 60*60*24 multiply with number of days you want to subtract and minus it from the current time



subtract 2 days to the current date.

date('Y-m-d',time()-3600*24*2);


subtract 3 days to the current date.

date('Y-m-d',time()-3600*24*3);



strtotime()
By using strtotime() function in php
strtotime() function convert string to time .

subtract 1 days to the current date.

date('Y-m-d',strtotime("-1 days"));


subtract 2 days to the current date.

date('Y-m-d',strtotime("-2 days"));


subtract 3 days to the current date.

date('Y-m-d',strtotime("-3 days"));



subtract 1 days to any date.

date('Y-m-d',strtotime("2021-10-19 -1 days"));


subtract 2 days on any date.

date('Y-m-d',strtotime("2021-10-19 -2 days"));


subtract 3 days to any date.

date('Y-m-d',strtotime("2021-10-19 -3 days"));


Post Answer