lordvader Posted March 20, 2008 Share Posted March 20, 2008 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 Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted March 20, 2008 Share Posted March 20, 2008 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. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 20, 2008 Share Posted March 20, 2008 You could do an array <?php $months = array("","January","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec"); $i = 1; while($i <13){ echo $months[$i]."<br />"; } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 20, 2008 Share Posted March 20, 2008 You need to do something like this: <?php for ($i=1;$i<13;$i++) echo date('F',strtotime('2008-' . $i . '-01')); . '<br>'; ?> Ken Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 20, 2008 Share Posted March 20, 2008 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 20, 2008 Share Posted March 20, 2008 No, if the date() function can't figure out what the second argument is supposed to be, it defaults to "December 31, 1969". Ken Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 20, 2008 Share Posted March 20, 2008 An array of names using a foreach() loop to output them would be the quickest executing code. Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted March 20, 2008 Share Posted March 20, 2008 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"); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 20, 2008 Share Posted March 20, 2008 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 Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted March 20, 2008 Share Posted March 20, 2008 no this is easier $i = 1; while ($i <= 12) { echo date('F', strtotime($i.'/1/2008')) . ":<br />"; $i++; } Quote Link to comment Share on other sites More sharing options...
lordvader Posted March 20, 2008 Author Share Posted March 20, 2008 Ah, thanks everybody! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 20, 2008 Share Posted March 20, 2008 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 Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted March 20, 2008 Share Posted March 20, 2008 u win Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.