Jump to content

[SOLVED] Help Please date calculation


rugzo

Recommended Posts

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...

 

 

Link to comment
Share on other sites

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]

 

Link to comment
Share on other sites

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.

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.