stat_base.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/5/25
  6. * Time: 上午9:24
  7. */
  8. namespace statistics;
  9. use Exception;
  10. use DateTime;
  11. use DateInterval;
  12. class stat_base
  13. {
  14. const cur_date_type = 1;
  15. const last_week_type = 2;
  16. const last_hmonth_type = 3;
  17. const last_month_type = 4;
  18. const admin_member_id = 36429;
  19. protected $mStartm;
  20. protected $mEndtm;
  21. protected $mDateId;
  22. protected $mModel;
  23. public function __construct($stime)
  24. {
  25. $this->mModel = Model('stat_daily');
  26. $stime = intval($stime);
  27. $this->mDateId = strtotime(date('Y-m-d',$stime));
  28. if($stime <= 0 || $this->mDateId === false) {
  29. throw new Exception(__METHOD__ . ' 输入时间错误');
  30. }
  31. }
  32. protected function calc_time($stype)
  33. {
  34. $stime = $this->mDateId;
  35. if($stype == stat_base::cur_date_type)
  36. {
  37. $this->mStartm = strtotime(date('Y-m-d',$stime));
  38. $this->mEndtm = $this->mStartm + 86400 - 1;
  39. }
  40. elseif($stype == stat_base::last_week_type)
  41. {
  42. $coord_time = strtotime(date('Y-m-d',$stime)) + 86400;
  43. $this->mStartm = $coord_time - 86400 * 7;
  44. $this->mEndtm = $coord_time -1;
  45. }
  46. elseif($stype == stat_base::last_hmonth_type)
  47. {
  48. $coord_time = strtotime(date('Y-m-d',$stime)) + 86400;
  49. $this->mStartm = $coord_time - 86400 * 15;
  50. $this->mEndtm = $coord_time -1;
  51. }
  52. elseif($stype == stat_base::last_month_type)
  53. {
  54. $cur_time = strtotime(date('Y-m-d',$stime)) + 86400;
  55. $cur_date = new DateTime();
  56. $cur_date->setTimestamp($cur_time);
  57. $inter = new DateInterval('P1M');
  58. $cur_date->sub($inter);
  59. $this->mStartm = $cur_date->getTimestamp();
  60. $this->mEndtm = $cur_time - 1;
  61. }
  62. else {
  63. return false;
  64. }
  65. return true;
  66. }
  67. protected function save($fEdit,$datas)
  68. {
  69. if($fEdit) {
  70. $ret = $this->mModel->where(['date_id' => $this->mDateId])->update($datas);
  71. } else {
  72. $datas['date_id'] = $this->mDateId;
  73. $ret = $this->mModel->insert($datas);
  74. }
  75. return $ret;
  76. }
  77. }