rugzo Posted June 29, 2009 Share Posted June 29, 2009 Hi all, i have a problem by date calculation. $buayisim2 = date('M-Y',mktime(0,0,0,date("m")-2)) ; $buayisim3 = date('M-Y',mktime(0,0,0,date("m")-3)) ; $buayisim4 = date('M-Y',mktime(0,0,0,date("m")-4)) ; $buayisim5 = date('M-Y',mktime(0,0,0,date("m")-5)) ; $buayisim6 = date('M-Y',mktime(0,0,0,date("m")-6)) ; the above code works fine except $buayisim4. $buayisim2 reproduces Apr-2009 $buayisim3 reproduces Mar-2009 $buayisim4 reproduces Mar-2009 $buayisim5 reproduces Jan-2009 $buayisim6 reproduces Dec-2008 Whatever i do i don't get the february. 4 should produce february but instead of that i get march like the 3. Does anyone have an idea ?? thanks... Quote Link to comment Share on other sites More sharing options...
Cosizzle Posted June 29, 2009 Share Posted June 29, 2009 This is a bug in PHP http://bugs.php.net/bug.php?id=27789 I think it has something to do with the leap year Also a bit off topic it might be useful to throw all this into an array and print it out (not sure the direction you were going with this, but good practice to do so) $buayisim = array(); $buayisim[] = date('M-Y',mktime(0,0,0,date("m")-2)) ; $buayisim[] = date('M-Y',mktime(0,0,0,date("m")-3)) ; $buayisim[] = date('M-Y',mktime(0,0,0,date("m")-4)) ; $buayisim[] = date('M-Y',mktime(0,0,0,date("m")-5)) ; $buayisim[] = date('M-Y',mktime(0,0,0,date("m")-6)) ; echo '<pre>'; print_r($buayisim); echo '</pre>'; echo $buayisim[3] Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 29, 2009 Share Posted June 29, 2009 As it states at the bottom of that bug report, this is not a bug. The mktime() function uses the current values for any values you don't specify. So, in the posted code, it is using the current day and year. When you give it any month that does not contain the current day number in it, it goes to the first day of the following month. Just force it to use a day that exists in all months instead of the current day - $buayisim2 = date('M-Y',mktime(0,0,0,date("m")-2,1)); And you may have a problem with octal numbers and might need to use date("n"), which will eliminate the leading zero so the numbers won't appear to be octal. Quote Link to comment Share on other sites More sharing options...
rugzo Posted June 29, 2009 Author Share Posted June 29, 2009 Thank you PFMaBiSmAd, you'r the man it worked. Thank you very much... 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.