phpdate

PHP get the current month of a date


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

Any ideas? thanks.


Solution

  • You need to use the default date() function of PHP to get current month. Then you can easily check it by if conditions as mentioned in the code below:

    <?php 
    $month = date('n'); // 'n' represents a numeric representation of a month, without leading zeros (1 to 12)
    
    if($month == 12){
       echo "<br />December is the month :)";
    } else {
       echo "<br /> The month is probably not December";
    }
    ?>