2013-12-18 176 views
0

大家好,我想获取当前日期的月份。PHP获取日期的当前月份

这是我的尝试:

<?php 

    $transdate = date('m-d-Y', time()); 

    echo $transdate; 

    $month = date('m', strtotime($transdate)); 

    if($month == "12"){ 

echo "<br />December is the month :)"; 
    } else { 
echo "<br /> The month is probably not December"; 
    } 

    ?> 

但结果是错误的,它应该显示十二月是本月:0

任何想法?谢谢。

+1

它对我来说工作正常 –

+0

为什么你不运行第二个日期功能与时间()作为​​第二个参数,你运行两次? :? –

+1

'if($ month == 12)'。 12应该是整数不是字符串 –

回答

3

这个代码将用于工作,你

<?php 

    $transdate = date('m-d-Y', time()); 

    echo $transdate; 

    $month = date('m'); 

    if($month == 12){ 

echo "<br />December is the month :)"; 
    } else { 
echo "<br /> The month is probably not December"; 
    } 
?> 
0

试试这个

$transdate = date('m-d-Y', time()); 

     $d = date_parse_from_format("m-d-y",$transdate); 



    $month = $d["month"]; 

     if($month == "12"){ 

    echo "December is the month :)"; 
     } else { 
    echo "The month is probably not December"; 
     } 
?> 
0

这是做这个

echo date("F", strtotime('m')); 
0

为什么不干脆

echo date('F, Y'); 
的一种更好的方式

?这打印2017年11月。

相关问题