Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: date function to get month of the current date

Tags:

php

datetime

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

like image 474
iamjonesy Avatar asked Sep 22 '10 09:09

iamjonesy


People also ask

How can I get current month in php?

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"; } ?>

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

The Excel MONTH function extracts the month from a given date as number between 1 to 12. You can use the MONTH function to extract a month number from a date into a cell, or to feed a month number into another function like the...

How can I get current month number?

Use the new Date() constructor to get a date object. Call the getMonth() method on the object and add 1 to the result. The getMonth method returns a zero-based month index so adding 1 returns the current month.


5 Answers

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.

like image 62
fabrik Avatar answered Oct 02 '22 19:10

fabrik


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.

like image 32
oezi Avatar answered Oct 02 '22 20:10

oezi


$unixtime = strtotime($test);
echo date('m', $unixtime); //month
echo date('d', $unixtime); 
echo date('y', $unixtime );
like image 29
Pramendra Gupta Avatar answered Oct 02 '22 18:10

Pramendra Gupta


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"
like image 24
Hannes Avatar answered Oct 02 '22 19:10

Hannes


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
?>
like image 25
J-a-n-u-s Avatar answered Oct 02 '22 19:10

J-a-n-u-s