user2871190
user2871190

Reputation: 251

PHP - number of month from date

I'm trying to get the number of a month from a date using this:

$newDate = date("Y-m-d", strtotime('2014-05-04'));  
$Month = date('n', $newDate);
echo $Month;

It returns 1 (this is January...) How is this possible? It should return 0.

I used the date format because of that thread:

PHP: date function to get month of the date

I hope somebody can help me.

Thanks in advance

shivan

Upvotes: 0

Views: 579

Answers (2)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

For datetime operation you should use DateTime class in PHP

$date = new DateTime('2014-05-04');
echo $date->format('n');

Upvotes: 0

Ejaz
Ejaz

Reputation: 8872

$Month = date('m', strtotime('2014-05-04'));

you can get the month like this

Upvotes: 2

Related Questions