Error with strtotime php

echo date("m", strtotime("january")); 

Returns 01 as expected

 echo date("m", strtotime("february")); 

But it returns 03

Has anyone else encountered this problem?

PHP Version 5.1.6

+6
source share
2 answers

Today is the 29th. This February is not the 29th, and because you did not specify a day in February, it uses "today." The strtotime function uses relative dates, so February 29 is basically March 1 of this year.

To solve your problem:

 echo date("m", strtotime("February 1")); 
+24
source

As strtotime () handles only date formats in English, you can try using this function, which I just made for you. With this, you can also process month names in other languages.

I don’t know if this is important for your application, but now you have it.

 function getMonth($month, $leadingZero = true) { $month = strtolower(trim($month)); // Normalize $months = array('january' => '1', 'february' => '2', 'march' => '3', 'april' => '4', 'may' => '5', 'june' => '6', 'july' => '7', 'august' => '8', 'september' => '9', 'october' => '10', 'november' => '11', 'december' => '12', 'dezember' => '12', // German abrevation 'marts' => '3', // Danish abrevation for March ); if(isset($months[$month])) { return $leadingZero ? substr('0' . $months[$month], -2) : $months[$month]; } else { return false; } } 
0
source

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


All Articles