|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-11-03 10:38 UTC] wouter at deinternetjongens dot nl
Description:
------------
We came across a `bug` when trying to get the last weekday of a month.
When we first setted a new date and afterwards modified it to get the last weekday of that month it returns the last weekday of the month before the month given.
Test script:
---------------
$datetime = new DateTime()->setDate(2017, 12, 1);
var_dump($datetime->modify('last weekday this month');
Expected result:
----------------
DateTime {#12086
+"date": "2017-12-30 00:00:00.000000",
+"timezone_type": 3,
+"timezone": "UTC",
}
Actual result:
--------------
DateTime {#12086
+"date": "2017-11-30 00:00:00.000000",
+"timezone_type": 3,
+"timezone": "UTC",
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 16:00:01 2025 UTC |
Expected result should be : ---------------- DateTime {#12086 +"date": "2017-12-29 00:00:00.000000", +"timezone_type": 3, +"timezone": "UTC", }Hi! The only thing it did was: 'last weekday'. Whatever you added next, didn't have much effect (this month doesn't modify it, and is executed first). Last weekday literally means: going back in time, day by day, till a day has been found which is not a weekend day. So what you can do is this sequence of modifications: // Get the very last day in this month. $datetime->modify('last day of this month'); // Add one day: $datetime->modify('+1 day') // Find the last weekday before this day (effectively the first day of next month). $datetime->modify('last weekday'); Or: but this is very unreadable to anyone coming next: $datetime->modify(last weekday first day of next month'); This will first go to next month, then get the first day of that month, then find the last weekday before that.