php - 将简短形式的月份名称转换为月份编号[重复]

原文 标签 php

Convert short form month name to month number [duplicate]

$month_name = 'Feb';
$month_number = date("m", strtotime($month_name));

From above code, I get the output is 03 not 02. Why?

Answer

I suppose it's caused by current date and default value of date in strtotime function. Today we have 31 December so if you use strtotime, default value of date will be 31 Dec 2015, but if you change month to Feb the date will be 03 Mar 2015. Solution here is to add first day number at the begining e.g.

$month_name = '1 Feb';
$month_number = date("m", strtotime($month_name));

翻译