当日期格式为-日-月-年时,如何从日期查找月份


How to find out the month from a date when the date format is - day-month-year?

请告诉我,当日期格式如下时,如何从日期中找出月份?

    01/12/2011
  Day-Month-year

谢谢:)

$thedate = '01/12/2011';
$date = DateTime::createFromFormat('d/m/Y', $thedate);
print($date->format('m'));

DateTime对象只在php5.2和php5.2.3的createFromFormat()中可用。

或者您可以将时间戳设置为正确的格式:

$thedate = str_replace('/', '-', '01/12/2011');
$date = strtotime($thedate);
print(date('m', $date));

Try

$v = explode('/', $date);
echo $v[1];

$month = explode('/', $date)[1];

我还写过:<罢工>

$t = strtotime($date);
echo date('m', $t);

这将不起作用,因为格式12/10/2012中的日期被视为英文日期,因此第二部分10被视为月号而不是月号。你应该使用

DateTime::createFromFormat('d/m/Y', $date)