Dates showing up wrong on 31st of the month. |
Hi all,
This section has been working fine for a while but I have looked at it today on my test server and I noticed that it is broken and I have no idea why. Here is my function: PHP Code: public function get_month_page_views($month_num, $total = false, $column){ And I am using this to get the last 5 months statistics like : PHP Code: $data['last_month_views'] = $this->Analytics_model->get_month_page_views('-1 months', true, 'page_hits'); now as of the 31st of May the last 5 months dates are showing as : 1:2017-05-01 00:00:00 2:2017-03-01 00:00:00 3:2017-03-01 00:00:00 4:2017-01-01 00:00:00 5:2016-12-01 00:00:00 I cannot for the life of me figure out why its pulling March for both -1 months and -2 months and then messing up for the remained. Can anyone see an issue with this?
There was a thing in php where strtotime gives the wrong answer at the end or beginning of months for +1 month and -1 month etc. It was, if I remember correctly, something like on April 30th the first day of the previous month was given as Feb 1, not April 1, because the + or - month functionality had a flaw of some sort in it.
However, I can't remember the exact details as I thought it was fixed in php 5.3+ but your problem really reminds me of that issue. The datetime class is much better, easier and more powerful now anyway. So I would suggest not using strtotime but the datetime class http://php.net/manual/en/class.datetime.php to work out your required months. Can you show how you are calculating -1 months in php? Might be more revealing.
I have been playing around with the following just in a php section on a page.
PHP Code: $date = new DateTime('FIRST DAY OF -2 MONTH'); and this seems to be working fine but I am now trying to figure out how to format it into my function and keep the versatility. Doomie
In your original posted function you are sending something like '-1 months' and setting the variables:
PHP Code: $pm = (int) date('n', strtotime($month_num)); Where 'n' is 1-12 and 'Y' is full year like 2017. So just change it to (and I am not sure if you mean by -1 months to include last month or just to cover the current month) PHP Code: $date = new DateTime('FIRST DAY OF '.$month_num); Nothing will have changed apart from your dates should all work now. PS have not tested this just wrote it out so might be a bit buggy. In theory it is right. Edit: Just tested it on phpfiddle and it is working fine, but can't remember my login so can't post a link :-) Just paste this in and you will see it: http://phpfiddle.org/ PHP Code: <?php |
Welcome Guest, Not a member yet? Register Sign In |