繁体   English   中英

当日期格式为-day-month-year时,如何从日期中找出月份?

[英]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'));

仅PHP的DateTime对象可用于PHP 5.2,而createFromFormat() PHP 5.2.3。

或者,您可以将时间戳伪造为正确的格式:

$thedate = str_replace('/', '-', '01/12/2011');

$date = strtotime($thedate);
print(date('m', $date));

尝试

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

在PHP 5.4+中:

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

我还写道:

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

但这自12/10/201210日格式的日期起将不起作用 ,因此将第二部分10视为月份中的日期,而不是月份号,将其视为英语日期。 你应该用

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

正如@PeeHaa建议的那样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM