subtraction of error months in 31 days (strtotime)

Asked

Viewed 41 times

-2

I’m developing a graph using the library chart.js. It is working perfectly. It happens that today, the month of May with 31 days, the subtractions of the months happens in the wrong way.

$mhoje = date("M");

$m1 = date('M', strtotime(' - 1 month'));
$em1 = ucfirst(strftime('%b', strtotime($m1)));

echo "$mhoje / $m1 / $em1"; // May / May / Mai

That is to say, hoje menos 1 month it’s still May.

I use only date("M") because I only need the name of the month and not the dates, but I have already tested complete, as date("Y-M-d").

Other days it works perfectly.

I need to get the last 12 months from today’s date.

1 answer

3


According to the documentation: https://www.php.net/manual/en/datetime.formats.relative.php

To get the last day of a month you must use last day, you can see in the documentation link where this spelled "Day-based Notations", or below:

Format Description Exams
yesterday Midnight of last night yesterday 14:00
midnight Time is set to 00:00:00
today Time is set to 00:00:00
now Now - this is simply ignored
noon Time is set to 12:00:00 yesterday noon
tomorrow Midnight of tomorrow
back of hour 15 minutes after the reported time back of 7pm, back of 15
front of hour 15 minutes before the reported time front of 5am, front of 23
first day of Set the day to the first day of the current month. This phrase is best used, along with a month name. first day of January 2008
last day of Sets the day for the last of the current month. This phrase is best used, along with a month name. last day of next month
ordinal space dayname space of Calculates the ordinal number x-th of the current month week. first sat of July 2008
last space dayname space of Calculate the last day of the week of the current month. last sat of July 2008
number space? (unit | 'week') Handles relative portions of time where the value is a number. +5 weeks, 12 day, -7 weekdays
ordinal space Unit Handles relative portions of time where the value is a text. fifth day, second month
ago Denies all values of previous relative portions of time. 2 days ago, 8 days ago 14:00, 2 months 5 days ago, 2 months ago 5 days, 2 days ago
dayname Move to the next day of this name. Monday
reltext space week Manipulates the special format weekday + last/this/next week. Monday next week

And to get the previous month you must use previous month, in the documentation locates where it is written Used Symbols, or below:

Description Format
dayname sunday | monday | tuesday | wednesday | thursday | friday | saturday | sun | mon | tue | wed | thu | fri | sat
daytext weekday | weekdays
number [+-]?[0-9]+
ordinal first | second | third | fourth | fifth | sixth | seventh | eighth | ninth | tenth | eleventh | twelfth | next | last | previous | this
reltext next | last | previous | this
space [ \t]+
Unit (('sec' | 'second' | 'min' | 'minute' | 'hour' | 'day' | 'fortnight' | 'forthnight' | 'month' | 'year') 's'?) | weeks | daytext

Soon you can combine both in:

last day of previous month

Translating, "the last day of the previous month". The use of -1 month should be due to considering 30 days as the unit of value, see the online test: https://ideone.com/CwMD39

  • I tested and it worked. I believe I failed in formulating the question. I need to get the last 12 months. last day of previous month i get the previous month correctly. But when I need to get the whole year?

  • 1

    @Juniorlike make a loop, if the current month is 5 you will make 4 loops running the relative time obtained and catching all last days of all 4 previous months, you can catch the current month in number format using $mesatual = date('n'), then saves the value in a variable and makes -1 and plays in a loop for ($i = $mesatual - 1; $i > 0; $i--) { ... }

  • Thanks for the broad and quick response!

  • @Juniorlike in strtotime in the second parameter passes the value of the last date obtained from the previous loop, and can save in an array at each loop.

  • Yes, I saw your code but I thought about putting an array together, I think it will be better for me to work with the library.

  • in his for example it always stops in the month 1. It does not come back to the month 12, 11 and so on. Could you help me, please? Or I open a new question?

  • @Juniorlike as I said in the previous comment, you have to save the value returned from strtotime to a variable, which will be a "Unix timestamp", and put in the second parameter of the next strtotime.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.