PHP 获取日期的当前月份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20659389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:23:25  来源:igfitidea点击:

PHP get the current month of a date

php

提问by Dunkey

Hello everybody I would like to get the current month of a date.

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

This is what I tried:

这是我尝试过的:

   <?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";
   }

    ?>

But the result is wrong, it should display December is the month :0

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

Any ideas? thanks.

有任何想法吗?谢谢。

回答by Anil Meena

this code will work for you

这段代码对你有用

<?php 
$month = date('m');

if($month == 12){
   echo "<br />December is the month :)";
} else {
   echo "<br /> The month is probably not December";
}
?>

回答by Azima

Why not simply

为什么不简单

echo date('F, Y');

? This prints November, 2017.

? 打印时间为 2017 年 11 月。

回答by user7789076

Try this

尝试这个

  $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";
       }
?>

回答by Kingsley Mitchell

This is a much better way of doing this

这是一个更好的方法

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