PHP get the current month of a date
Asked Answered
S

6

24

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.

Suzannasuzanne answered 18/12, 2013 at 13:22 Comment(5)
Why you don't run the second date with function with the time() as a second parameter and you run it two times ? :?Dennison
if($month == 12). 12 should be integer not stringPursuant
possible duplicate of Get month of a given dateIngroup
switch the else and ifLatinism
@raheelshan date() returns a string, so you better not wrap 12 in quotes. BTW, when comparing string "12" and interger 12 with == operator, PHP will ignore data type and regard them as the same. === operator would be more strict.Janayjanaya
C
65

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('m');

if($month == 12){
   echo "<br />December is the month :)";
} else {
   echo "<br /> The month is probably not December";
}
?>
Corrida answered 18/12, 2013 at 13:39 Comment(1)
date('m'); gets a numeric representation of the month (from 01 to 12). In case you want a numeric representation of the month, without leading zeros: date('n');Silviasilviculture
B
11

Why not simply

echo date('F, Y');

? This prints November, 2017.

Bernardina answered 17/11, 2017 at 11:22 Comment(0)
C
9

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

$month = idate("m");
Cultivable answered 7/9, 2020 at 20:39 Comment(0)
G
5

This is a much better way of doing this

echo date("F", strtotime('m'));
Gnathonic answered 2/10, 2017 at 2:17 Comment(2)
this is a more solid answerZita
Thank you Albuquerque, remember to upvote too :)Gnathonic
M
0

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";
       }
?>
Manatee answered 18/12, 2013 at 13:37 Comment(1)
probably doesn't inspire confidence, but it's so realistic - since date could easily be spoofed! :D that's greatGeorginageorgine
C
0

I can see some time has passed since this post but currently the best way of working with dates is picking the right lettering from the table that PHP has: datetime.format.php

You can get just the month with lowercase n:

date('n')

Here are all the month options:

  1. F A full textual representation of a month, such as January or March January through December
  2. m Numeric representation of a month, with leading zeros 01 through 12
  3. M A short textual representation of a month, three letters Jan through Dec
  4. n Numeric representation of a month, without leading zeros 1 through 12
  5. t Number of days in the given month 28 through 31
Chafee answered 11/1 at 23:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.