PHP 获取日期的当前月份

2022-08-30 18:43:49

大家好,我想得到一个日期的当前月份。

这是我尝试过的:

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

?>

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

有什么想法吗?谢谢。


答案 1

您需要使用 PHP 的默认 date() 函数来获取当前月份。然后,您可以通过以下代码中提到的条件轻松检查它:

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

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

答案 2

为什么不干脆

echo date('F, Y');

?这是2017年11月的印刷品。


推荐