Hướng dẫn minus month php
I'm trying to subtract 1 month from a date.
This gives: 08-2016 How can I subtract a month to get
Cœur 35.5k24 gold badges188 silver badges257 bronze badges asked Aug 3, 2016 at 8:55
3
output
answered Aug 3, 2016 at 9:00
Passionate CoderPassionate Coder 7,0262 gold badges17 silver badges37 bronze badges 4 Warning! The above-mentioned examples won't work if call them at the end of a month.
will output:
The following example will produce the same result:
Plenty of ways how to solve the issue can be found in another thread: PHP DateTime::modify adding and subtracting months answered Oct 10, 2017 at 12:54
Alexey KosovAlexey Kosov 2,8832 gold badges20 silver badges31 bronze badges 1 Try this,
answered Aug 3, 2016 at 9:00
Vinod VTVinod VT 6,69010 gold badges52 silver badges74 bronze badges 3 Depending on your PHP version you can use DateTime object (introduced in PHP 5.2 if I remember correctly):
You can pass another date to the constructor, it does not have to be the current date. More information: http://php.net/manual/en/datetime.modify.php answered Aug 3, 2016 at 9:09
I used this to prevent the "last days of month"-error. I just use a second strtotime() to set the date to the first day of the month:
answered Jul 31, 2020 at 10:31
MarcoMarco 3,2544 gold badges22 silver badges29 bronze badges
answered May 9, 2019 at 6:00
1
answered Jun 15, 2019 at 20:29
1
First change the date format m-Y to Y-m
This code minus 1 month to the given date
answered Jun 25, 2019 at 7:35
AngularJMKAngularJMK 1,08013 silver badges14 bronze badges Try this,
Android 1,4104 gold badges11 silver badges23 bronze badges answered Jun 26, 2019 at 10:08
This could be rewritten more elegantly, but it works for me answered Dec 29, 2019 at 7:53
1 |