How to get date month in php?

I want to be able to figure out the month of the current date variable. I'm ex vb.net and the way to do it there is just date.Month. How do I do this in PHP?

Thanks,

Jonesy

I used date_format($date, "m"); //01, 02..12

This is what I wanted, question now is how do I compare this to an int since $monthnumber = 01 just becomes 1

John

11.8k11 gold badges90 silver badges155 bronze badges

asked Sep 22, 2010 at 9:51

iamjonesyiamjonesy

24.1k40 gold badges134 silver badges206 bronze badges

2

See http://php.net/date

date('m') or date('n') or date('F') ...

Update

m Numeric representation of a month, with leading zeros 01 through 12

n Numeric representation of a month, without leading zeros 1 through 12

F Alphabetic representation of a month January through December

....see the docs link for even more options.

answered Sep 22, 2010 at 9:54

How to get date month in php?

What does your "data variable" look like? If it's like this:

$mydate = "2010-05-12 13:57:01";

You can simply do:

$month = date("m",strtotime($mydate));

For more information, take a look at date and strtotime.

EDIT:

To compare with an int, just do a date_format($date,"n"); which will give you the month without leading zero.

Alternatively, try one of these:

if((int)$month == 1)...
if(abs($month) == 1)...

Or something weird using ltrim, round, floor... but date_format() with "n" would be the best.

How to get date month in php?

Kwido

1,3728 silver badges21 bronze badges

answered Sep 22, 2010 at 9:56

oezioezi

50k10 gold badges97 silver badges115 bronze badges

1

$unixtime = strtotime($test);
echo date('m', $unixtime); //month
echo date('d', $unixtime); 
echo date('y', $unixtime );

answered Sep 22, 2010 at 9:56

Pramendra GuptaPramendra Gupta

14.3k4 gold badges32 silver badges34 bronze badges

as date_format uses the same format as date ( http://www.php.net/manual/en/function.date.php ) the "Numeric representation of a month, without leading zeros" is a lowercase n .. so

echo date('n'); // "9"

answered Sep 22, 2010 at 10:45

HannesHannes

7,9474 gold badges32 silver badges51 bronze badges

As it's not specified if you mean the system's current date or the date held in a variable, I'll answer for latter with an example.

<?php
$dateAsString = "Wed, 11 Apr 2018 19:00:00 -0500";

// This converts it to a unix timestamp so that the date() function can work with it.
$dateAsUnixTimestamp = strtotime($dateAsString);

// Output it month is various formats according to http://php.net/date

echo date('M',$dateAsUnixTimestamp);
// Will output Apr

echo date('n',$dateAsUnixTimestamp);
// Will output 4

echo date('m',$dateAsUnixTimestamp);
// Will output 04
?>

answered Apr 7, 2018 at 4:59

How to get date month in php?

J-a-n-u-sJ-a-n-u-s

1,33915 silver badges20 bronze badges

1

To compare with an int do this:

<?php
$date = date("m");
$dateToCompareTo = 05;
if (strval($date) == strval($dateToCompareTo)) {
    echo "They are the same";
}
?>

answered Feb 29, 2020 at 16:20

e102e102

657 bronze badges

How can I get month in PHP?

M - A short textual representation of a month (three letters) n - A numeric representation of a month, without leading zeros (1 to 12) t - The number of days in the given month.

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

You can be creative with this. For example, to get the first and last second of a month: $timestamp = strtotime('February 2012'); $first_second = date('m-01-Y 00:00:00', $timestamp); $last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!

How do you find the month from a date?

Get month from date.
Generic formula. =MONTH(date).
If you need to extract the month from a date, you can use the MONTH function. In the generic form of the formula above, the date must be in a form that Excel recognizes as a valid date..
The MONTH function takes just one argument, the date from which to extract the month..

How can I get end of month in PHP?

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