I have a problem with strtotime displaying months. See the code:

print(date('M-d')); //prints May-31
print(date('M-d',strtotime('-1 month'))); //prints May-01

Since i need it to get the previous month, why is the function ignoring the fact that there are 31 days in May?


This is a known problem, check this comment in the function page: http://www.php.net/manual/en/function.strtotime.php#107331

There is also a note that suggest to use DateTime::sub():

Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.