Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

first day != "first day of this month"

Tags:

php

datetime

There's a really strange behaviour in PHP when using "first day" to modify a date.

'first day' ' of'? Sets the day of the first of the current month. (http://php.net/manual/de/datetime.formats.relative.php)

$currentMonthDate = new DateTime("2014-07-30 10:26:00.000000");
echo $currentMonthDate->format('Ym');

$currentMonthDate->modify('first day');
echo $currentMonthDate->format('Ym');

201407 / 201407 OK

$currentMonthDate = new DateTime("2014-07-31 10:26:00.000000");
echo $currentMonthDate->format('Ym');

$currentMonthDate->modify('first day');
echo $currentMonthDate->format('Ym');

201407 / 201408 WHY?

$currentMonthDate = new DateTime("2014-08-01 10:26:00.000000");
echo $currentMonthDate->format('Ym');

$currentMonthDate->modify('first day');
echo $currentMonthDate->format('Ym');

201408 / 201408 OK

I found this behaviour on our production-server running PHP 5.2, so I assumed it's a ancient bug, but it occurs in PHP 5.3 (http://phptester.net/) and 5.5 on our test-server.

If I use "first day of this month" in PHP 5.2 the same behaviour occurs. In PHP 5.5 "first day of this month" works as expected.

Is the "first day"-behaviour a bug? And how to get the "first day of this month" in PHP 5.2 without doing weird conversions between string and date?

like image 520
Fabio Poloni Avatar asked Jul 31 '14 08:07

Fabio Poloni


People also ask

How do you find the first day of the month?

This means you can get the first day of the current month with a formula like this: =EOMONTH(A1,-1)+1. This formula "rolls back" a date in A1 to the last of the previous month, then adds 1. The result is the first day of the "current" month (i.e. first day of the month given by the date in A1).

What is the first day of the month given in A1?

This formula "rolls back" a date in A1 to the last of the previous month, then adds 1. The result is the first day of the "current" month (i.e. first day of the month given by the date in A1). Formulas are the key to getting things done in Excel.

How to calculate the first day of the month in Google Sheets?

Here the DAY Function returns the day number of a date. By subtracting this from the current date (and adding 1) we arrive at the first day of the month. These formulas work exactly the same in Google Sheets as in Excel.

How to extract the first day of the month in Excel?

Here, the formula contains the DAY function in order to extract the first day of a month. Excel stores dates as serial numbers. The day function returns a number from 1 to 31 to represent the month’s days.


1 Answers

It seems like this issue is a Documentation Problem.

From Derick in bug #51096: "first day" and "last day" should be "+1 day" and "-1 day".

The documentation should be updated to reflect this behaviour, as currently the "first/last day of" section says the "of" is optional when it is not.


UPDATE

The documentation is now fixed. So the ' of'? is not optional anymore.

'first day of' Sets the day of the first of the current month.

like image 178
Fabio Poloni Avatar answered Sep 29 '22 17:09

Fabio Poloni