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
https://forums.phpfreaks.com/topic/260081-mktime-issue-leap-year-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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