mrMarcus Posted March 31, 2012 Share Posted March 31, 2012 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 More sharing options...
PFMaBiSmAd Posted March 31, 2012 Share Posted March 31, 2012 date('d') ^^^ Since you are just doing year/month date math, you need to use a day number that exists in every month. Use '1' as a value for the day parameter instead of date('d'). Link to comment https://forums.phpfreaks.com/topic/260081-mktime-issue-leap-year-problem/#findComment-1333043 Share on other sites More sharing options...
mrMarcus Posted March 31, 2012 Author Share Posted March 31, 2012 Good catch. Thank you sir. Solved. Link to comment https://forums.phpfreaks.com/topic/260081-mktime-issue-leap-year-problem/#findComment-1333044 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.