How to Subtract Days With PHP
-
Using the
strtotime()
Method to Subtract Days in PHP -
Using the
DateTime()
to Subtract Days in PHP - Subtract From the Given Date in PHP
- Subtract Week From the Given Date in PHP
It is an important method for subtracting days, weeks, or months from a given date in PHP. The ways like we may use either PHP’s strtotime
method or the in-built DateTime
class to do.
The date()
and strtotime()
both function are use in PHP. Which makes it simple to subtract time (hours, minutes, and seconds) from a given date or current time (date).
The date()
method returns a prepared string after formatting a specific time.
On the other side turns a text formatted DateTime
into a Unix timestamp. date()
and strtotime()
can help subtract time from the current time (date) in PHP.
So here is the way to subtract time from a current DateTime
. Subtract 1
day with PHP form current date:
Using the strtotime()
Method to Subtract Days in PHP
Sample Code:
<?php
// current time in PHP
$datetime = date("Y-m-d ");
// print current time
echo $datetime;
echo "\n";
//After using of strotime fuction then result
$yesterday = date("Y-m-d", strtotime("yesterday"));
echo $yesterday;
?>
Output:
2021-12-06
2021-12-05
Above is an example of subtracting days from the current date by providing the string yesterday to strtotime
.
Using the DateTime()
to Subtract Days in PHP
Sample Code:
<?php
//New DateTime object representing current date.
$currentDate = new DateTime();
//Use the subtract function to subtract a DateInterval
$yesterdayTime = $currentDate->sub(new DateInterval('P1D'));
//Get yesterday date
$yesterday = $yesterdayTime->format('Y-m-d');
//Print yesterday date.
echo $yesterday;
?>
Output:
2021-12-05
We discussed PHP’s old version 5.3.0, using the DateInterval
class. And it represents a date period.
Now, we will discuss P1D
. We define the DateInterval
class’s objects to P1D
, which means one day (one-day period).
The interval can deduct from the given date and time. We can use P5D
(five days) instead of P1D
(one day) if you want to remove five days instead of one.
Subtract From the Given Date in PHP
Sample Code:
<?php
//Pass the date which you want to subtract from
//the $time parameter for DateTime.
$currentDate = new DateTime('2021-01-01');
//Subtract a day using DateInterval
$yesterdayTime = $currentDate->sub(new DateInterval('P1D'));
//Get the date in a YYYY-MM-DD format.
$yesterday = $yesterdayTime->format('Y-m-d');
//Print Date.
echo $yesterday;
?>
Output:
2020-12-31
Subtract Week From the Given Date in PHP
Using strtotime()
Sample Code:
<?php
//One week or 7 days ago
$lastWeekDate = date("Y-m-d", strtotime("-7 days"));
//OutPut
echo $lastWeekDate;
?>
Output:
2021-11-29
As we know that from the start time()
method, we can subtract time, day, month, and year from a given date.
The $interval
spec argument DateInterval
class for P1W
. Which stands for a time of one week, P1W=one week Period
. Now, If you want to Change P1W
(one week period) to P2W
to deduct two weeks, it will be a great approach.