How to subtract a number of days from todays date using PHP date function

H

How Can We Help?

How to subtract a number of days from todays date using PHP date function

$var = ‘2007-08-15’;
$var_subtracted_date = date(‘Y-m-d’, strtotime(‘-2 days’, strtotime($var)));

Explanation of PHP functions:

1)

FUNCTION
date

DESCRIPTION
string
 date ( string $format [, int $timestamp ] )

PARAMETERS USED IN THIS EXAMPLE

Y = A full numeric representation of a year, 4 digits (Examples: 1999 or 2003)
= Numeric representation of a month, with leading zeros (01 through 12)
= Day of the month, 2 digits with leading zeros (01 to 31)

2)

FUNCTION
strtotime

DESCRIPTION
int strtotime ( string $time [, int $now ] )

PARAMETERS

time The string to parse. Before PHP 5.0.0, microseconds weren’t allowed in the time, since PHP 5.0.0 they are allowed but ignored.
now The timestamp which is used as a base for the calculation of relative dates.

EXAMPLE

echo strtotime(“now”), “\n”;
echo strtotime(“10 September 2000”), “\n”;
echo strtotime(“+1 day”), “\n”;
echo strtotime(“+1 week”), “\n”;
echo strtotime(“+1 week 2 days 4 hours 2 seconds”), “\n”;
echo strtotime(“next Thursday”), “\n”;
echo strtotime(“last Monday”), “\n”;

About the author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me