Minus Date Php

This is a tutorial on how khổng lồ subtract days, weeks or months from a date using PHP.. To vị this, we can use either PHP’s strtotime function or the inbuilt DateTime class.

Bạn đang xem: Minus date php

Yesterday / -1 days.

To get yesterday’s date, you can pass in the string “yesterday” to lớn the strtotime function:

//Yesterday $yesterday = date("Y-m-d", strtotime("yesterday")); //On 2019-08-19, this resulted in 2019-08-18 echo $yesterday;

Passing in the string yesterday khổng lồ strtotime is essentially the same thing as subtracting -1 days from today’s date.

A DateTime alternative sầu, which you can use if your PHP.. version is 5.3.0 or above:

//New DateTime object representing today"s date. $currentDate = new DateTime(); //Use the sub function to lớn subtract a DateInterval $yesterdayDT = $currentDate->sub(new DateInterval("P1D")); //Get yesterday"s date in a YYYY-MM-DD format. $yesterday = $yesterdayDT->format("Y-m-d"); //Print it out. emang lại $yesterday;

The DateInterval class was introduced in PHPhường version 5.3.0 and its purpose is khổng lồ represent a date interval. In the example above sầu, we mix the DateInterval object to P1D, which means a period of one day. We then subtracted that interval from the current date. If you wanted to subtract five days instead of one day, then you would use P5D instead of P1D.

If you want lớn subtract a day from a given date:

//Pass the date you want lớn subtract from in as //the $time parameter for DateTime. $currentDate = new DateTime("2019-01-01"); //Subtract a day using DateInterval $yesterdayDT = $currentDate->sub(new DateInterval("P1D")); //Get the date in a YYYY-MM-DD format. $yesterday = $yesterdayDT->format("Y-m-d"); //Print it out. emang lại $yesterday;

In the example above, we subtracted one day from 2019-01-01. This results in 2018-12-31.

One week ago / -7 days.

Xem thêm: Các Kqh Là Gì - Thông Báo Mời Đấu Giá

To get last week’s date in PHP, we can use the strtotime function lượt thích so:

//7 days ago - last week. $lastWeek = date("Y-m-d", strtotime("-7 days")); //On 2019-08-19, this resulted in 2019-08-12 emang lại $lastWeek;

In the example above, we passed “-7 days” in as a parameter khổng lồ the strtotime function. This works because the strtotime function is pretty good at parsing textual date descriptions.

An example for those who prefer khổng lồ use PHP’s DateTime class:

//Today"s date. $currentDate = new DateTime(); //Subtract a day using DateInterval $lastWeekDT = $currentDate->sub(new DateInterval("P1W")); //Get the date in a YYYY-MM-DD format. $lastWeek = $lastWeekDT->format("Y-m-d"); //Print out the result. emang đến $lastWeek;

Above, we specified P1W as the $interval_spec parameter for DateInterval, which translates inlớn “a period of one week.” If you want to lớn subtract two weeks instead of one week, then change P1W to P2W.

Last month / -30 days.

Getting last month’s date can be a bit tricky. This is because the number of days in a month can vary. Example: October has 31 days, whereas September has 30 days.

Take the following example:

//Last month $lastMonth = date("Y-m-d", strtotime("-1 month")); //On 2019-08-19, this resulted in 2019-07-19 emang đến $lastMonth;

The snippet above sầu will work in most situations. However, on the 31st of October, it will actually return the 1st of October. This is because PHP’s strtotime function treats “-1 month” the same as “-30 days.”

If you’re looking khổng lồ get the previous month’s name or number, you might want to lớn try the following code sample instead:

//Get the first date of the previous month. eđến date("Y-m-d", strtotime("first day of previous month"));

If you were to lớn run the example above on the 31st of October, it should return the 1st of September.

As you can see, dealing with dates can be extremely tricky. Hopefully, the examples above helped you khổng lồ better understand how khổng lồ subtract from dates in PHP..


Add days to lớn a date in PHPhường. Subtract years from a PHP. date. Subtract hours from a timestamp using PHPhường.

Recent Posts

Do landlords in Irelvà have sầu to provide you with a vacuum cleaner? A simple guide to the housing crisis in Ireland. Who killed Tony Soprano? How to edit Green Screen videos using Sony Vegas. Did Tony Soprano die at the kết thúc of The Sopranos? Slang: What does “this slaps” mean? Paying tax on OnlyFans income in Ireland. Do you have to register as a business in Ireland?


Abandoned Websites Cars Code DayZ DIY Drones Facebook Fantasy Football Football Funny Gaming Gardening Instagram Interesting Articles Ireland JavaScript Linux MySQL Nginx Nostalgia Opinion People of the Internet PHPhường Tutorials Politics Programming Humor Resident Evil 2 Remake SEO / Webmaster Snapchat Stocks Sysadmin TikTok Tinder Tips & Tricks Uncategorized