PHP get the current month of a date

Viewed 125414

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.

5 Answers

Why not simply

echo date('F, Y');

? This prints November, 2017.

I suggest you use the function idate(), which returns an integer rather than a formatted string.

$month = idate("m");

This is a much better way of doing this

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