PHP get the current month of a date – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and date . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about 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('m');
if($month == 12){
echo "<br />December is the month :)";
} else {
echo "<br /> The month is probably not December";
}
?>
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'));
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";
}
?>