Jump to content

Why is date('F', $n) returning December only?


lordvader

Recommended Posts

I'm having some trouble with date()...

A simple while:

$i = 1;
while ($i <= 12) {
echo date('F', $i) . ":<br />";
$i++;
}

I thought the above would print a list of the names of every month... but instead it just prints December a whole bunch of times.

Similarly, I thought passing 'm' instead of 'F' would give me a list of: 01 02 03 04 etc, but instead it just gives me a bunch of 12's..

What's the deal?

 

tia

Link to comment
Share on other sites

The second argument of date() is supposed to be a timestamp, the number of seconds past January 1, 1970 midnight. So this loop checks the month when it's 1 to 12 seconds past midnight of January 1, 1970.

 

You are correct that the second parameter is a timestamp and that a timestamp is supposed to be the number of seconds past January 1, 1970 midnight. However, if the OP was to follow your logic I suspect he is scratching his head because his results are returning 'December' - not January. I suspect this has to do with Daylight savings being in effect.

Link to comment
Share on other sites

No, if the date() function can't figure out what the second argument is supposed to be, it defaults to "December 31, 1969".

 

Ken

I don't think that's why it says December, though. If I try date() with a timestamp of 0, I get Dec. 31, 1969 6:00 pm. My timezone is GMT -6.

 

And by the way, here's a better way to get an array starting at index 1:

 

$months = array("January" => 1,"Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");

Link to comment
Share on other sites

Using

<?php
$months = array("January" => 1,"Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
?>

will not product the array starting at index 1.

This

<?php
$months = array(1 => "January","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
?>

will.

 

Ken

Link to comment
Share on other sites

Both methods -

 

<?php
// one method
$s1 = microtime(true);
$i = 1;
while ($i <= 12) {
echo date('F', strtotime($i.'/1/2008')) . ":<br />";
$i++;
}
$e1 = microtime(true);

// another method
$s2 = microtime(true);
$months = array("January","February","March","April","May","June","July","August","September","October","November","December");
foreach($months as $month)
echo "$month:<br />";
$e2 = microtime(true);

// output the time
$t1 = $e1 - $s1;
$t2 = $e2 - $s2;
echo "First: $t1,<br />Second: $t2<br />";
?>

 

And the time for them:

 

First: 0.00055980682373047,

Second: 1.9073486328125E-5

 

560 micro seconds vs 19 micro seconds

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.