Marc Sanders
Marc Sanders

Reputation: 392

PHP date outputting incorrect month

If I pass for example ?month=$04 in the url and echo $date I keep receiving January and not what it should be (April). If I echo out $month I get 04 which is correct. This is the code I have been using:

if (isset($_GET['month']) && $_GET['month']!='') {
        $month = $_GET['month'];
        $date = date('F', $month);
}

echo $date;

For the life of me I can't figure out why it's not outputting correctly. Any help much appreciated.

Upvotes: 2

Views: 232

Answers (3)

Matt
Matt

Reputation: 7249

You need an actual date. Try something like echo date("F", mktime(0, 0, 0, 4, 0, 0));

Upvotes: 0

Pekka
Pekka

Reputation: 449385

Look at what you're doing here:

date('F', '04');

the second parameter to date() is a timestamp, starting January 1st, 1970. So what you are doing is specifying January 1st, 1970, 00:00:04 hours midnight.

What you want to do could be achieved e.g. like so:

$timestamp = strtotime ("2000-$month-01"); // 2000-04-01 will always be April
echo date('F', $timestamp);

Upvotes: 5

Hoàng Long
Hoàng Long

Reputation: 10848

It's because you are using the date() with the wrong parameter. $month must be a Unix timestamp. You can consider using mk_time() function.

Upvotes: 1

Related Questions