Jump to content

mktime() issue... leap year problem?


mrMarcus

Recommended Posts

Or am I just going crazy?

 

OK, I've been running a query that uses the snippet below to get the previous month.  It's :

 

$sql = "SELECT COUNT(`id`) as `counted` FROM `click_tracking` WHERE `date` LIKE '". date('Y-m', mktime(0, 0, 0, (date('m')-1), date('d'), date('Y'))) ."-%'";

 

Here's a snippet for people to try:

 

date_default_timezone_set('America/Toronto');
echo date('Y-m', mktime(0, 0, 0, (date('m')-1), date('d'), date('Y'))); //prints 2012-03 (March, which is incorrect; should be February- 2012-02)

 

That should have printed 2012-02, but instead, it prints 2012-03.

 

If I modify the mktime() to (date('m')+1):

 

date_default_timezone_set('America/Toronto');
echo date('Y-m', mktime(0, 0, 0, (date('m')+1), date('d'), date('Y'))); //prints 2012-05 (May, which is incorrect; should be April - 2012-04)

 

It prints 2012-05 which is incorrect for the given timezone.  Should be 2012-04

 

I'm thinking it's a leap year bug and will work itself out tomorrow (per my timezone, anyways).  So, when I use the mktime() function, it seems to be thinking we're in April already.  A standard date('Y-m') prints the correct YYYY-MM (2012-03) though.  Am I tripping out?

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.