-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.
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?– Junior Like
@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 loopfor ($i = $mesatual - 1; $i > 0; $i--) { ... }
– Guilherme Nascimento
Thanks for the broad and quick response!
– Junior Like
@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.
– Guilherme Nascimento
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.
– Junior Like
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?– Junior Like
@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.
– Guilherme Nascimento