Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP get the current month of a date

Tags:

date

php

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.

like image 250
Dunkey Avatar asked Dec 18 '13 13:12

Dunkey


People also ask

How can I get start date and end of month in php?

php echo 'First Date = ' . date('Y-m-01') . '<br />'; echo 'Last Date = ' . date('Y-m-t') .

Which function is used to get only the month portion from a date?

MONTH function in Excel - get month number from date For example: =MONTH(A2) - returns the month of a date in cell A2.

How do I get the last day of the current month in php?

$date = strtotime ( $datestring ); // Last date of current month. $day = date ( "l" , $lastdate );

How do you get the first day of the month in php?

$first_day = date('Y-m-01'); $last_day = date('Y-m-t'); Php Get First Day Of Month. There are a number of different approaches that can be taken to solve the same problem. The following paragraphs will examine the various alternative approaches.


1 Answers

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";
}
?>
like image 50
Anil Meena Avatar answered Oct 29 '22 04:10

Anil Meena