php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75477 last weekday of this month returns previous month when date is set
Submitted: 2017-11-03 10:38 UTC Modified: 2021-04-06 17:25 UTC
From: wouter at deinternetjongens dot nl Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.1.11 OS: Linux, mac os
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: wouter at deinternetjongens dot nl
New email:
PHP Version: OS:

 

 [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",
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-03 10:40 UTC] wouter at deinternetjongens dot nl
Expected result should be :
----------------
DateTime {#12086
  +"date": "2017-12-29 00:00:00.000000",
  +"timezone_type": 3,
  +"timezone": "UTC",
}
 [2017-11-03 11:15 UTC] sjon at hortensius dot net
This is also described in the manual: http://php.net/manual/en/datetime.formats.relative.php - so this is basically a feature request.

You could use `last friday of December 2017` instead, see https://3v4l.org/cVpin
 [2017-11-03 11:50 UTC] wouter at deinternetjonges dot nl
Ok, so this is expected behaviour.

What we are trying to accomplish is getting the last weekday of a month.
So if it is a day between Monday till Friday I want to get the last day of those in a month.

Hope you get where I want to go to with this.
If it still is a feature request how can I ask for something like this to be added ?
 [2017-11-27 20:59 UTC] vandevreken dot hannes at gmail dot com
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.
 [2021-04-06 17:25 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2021-04-06 17:25 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is expected behaviour, and Hannes suggestion is the right workaround.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC