Problem with date() "M" format

I’m having the most difficult time getting the date to print May 17th, 2010 with the month (“F”) format being the issue. I’m having to get it to say November (the current month) but if I subtract from that it turns into an integer. Any help would be most appreciated! Thank you!

[php]
// Display the following date using the date() function: “May 24th, 2010”

$month = date (“F”, time()) - 6;
$day = date (“D”, time()) + 17;
$year = date (“Y”, time()) - 2;

echo $month," ", $day ,"th, ", $year;
[/php]

You should use functions such as mktime() or strtotime() e.g.

[php]
$month = date(“F”, strtotime("-6 months"));
var_dump($month);
[/php]

Here is another example using mktime()

[php]
$month = date(“F”, mktime(0, 0, 0, date(‘m’) - 6, date(‘d’), date(‘Y’)));
var_dump($month);
[/php]

Hey thank you for your response.

That does result in the month being illustrated as “May” rather than “-6” which was happening with my code above however why is it that my code results as such? It seems to match the parameters outlined in php.net? To test, I used codepad.org and I came up with the same result

I don’t know what you are referring to on php.net but it certainly is not correct. By taking a string “November” and subtracting 6 you are converting the string to an integer (0) which gives you 0 - 6 = -6

Ahhh, yes. That makes sense. Thank you for explaining the obvious! I’ve been hitting my head with this one for the last few days!

Sponsor our Newsletter | Privacy Policy | Terms of Service