• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dates showing up wrong on 31st of the month.

#1
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){
 
   $pm = (int) date('n'strtotime($month_num));
 
   $pmy = (int) date('Y'strtotime($month_num)); 
 
   $this->db->select('*');
 
   $this->db->from('analytics_page_views');
 
   $this->db->where('MONTH(date)'$pm);
 
   $this->db->where('YEAR(date)'$pmy);
 
   if ($total == 'true'){
 
   $this->db->select_sum($column);
 
   }
 
   $query $this->db->get();
 
   if ($total == 'true'){
 
   return $query->row();
 
   } else {
 
   return $query->result();    
    
}
 
   
  


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?
Reply

#2
@doomie22,

As a suggestion, why don't you send the values of $pm and $pmy as well as the executed SQL statement to the log so that you can see what the values are.
Reply

#3
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.
Reply

#4
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');
echo 
$date->format('m'); 

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
Reply

#5
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));
 
   $pmy = (int) date('Y'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);
$pm  $date->format('n'); 
$pmy $date->format('Y'); 

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
$month_num 
'-1 months';

$date = new DateTime('FIRST DAY OF '.$month_num);
$pm  $date->format('n'); 
$pmy $date->format('Y'); 

echo 
$pm;
echo 
'<br />';
echo 
$pmy;

?>
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.