📜 ⬆️ ⬇️

Date Modification Problem via strtotime

Good afternoon habravchane.

I want to tell you about a small problem with calculating the date function strtotime .

The essence is as follows. I needed to get the name of the previous month. There is nothing difficult in this problem:
$t = strtotime('-1 month'); echo strftime('%B', $t); 

In most cases, this option is great. But not today. But still not even Friday =). Because today is May 31, then the above code returned May 1. And +1 month returned in general on July 1. Strange. At such times, every other PHP programmer begins to reinvent the next bike:
 $months = array(1=>'','',''...); echo $months[date('m')-1]; 

')
But it can be made much simpler:
 $t = strtotime('first day of previous month'); echo strftime('%B', $t); 

This code just returns on April 1. Learn more about the format of the first argument to the strtotime function.

Beautiful code and good luck.

Source: https://habr.com/ru/post/144959/


All Articles