tags:

views:

64

answers:

4

Hi,

I want to be able to figure out the month of 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

+4  A: 

See http://php.net/date

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

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

fabrik
+2  A: 

how doeas you "data variable" look like? if its 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 those:

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

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

oezi
thanks i used date_format($date, "m") to get 01, 02..12. I've updated my question though because this has given me another question.
iamjonesy
+1  A: 
$unixtime = strtotime($test);
echo date('m', $unixtime); //month
echo date('d', $unixtime); 
echo date('y', $unixtime );
JapanPro
+1  A: 

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"
Hannes