2 // +----------------------------------------------------------------------+
 
   3 // | Anuko Time Tracker
 
   4 // +----------------------------------------------------------------------+
 
   5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
 
   6 // +----------------------------------------------------------------------+
 
   7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
 
   8 // | by anyone for any purpose, and freely redistributed alone or in
 
   9 // | combination with other software, provided that the license is obeyed.
 
  11 // | There are only two ways to violate the license:
 
  13 // | 1. To redistribute this code in source form, with the copyright
 
  14 // |    notice or license removed or altered. (Distributing in compiled
 
  15 // |    forms without embedded copyright notices is permitted).
 
  17 // | 2. To redistribute modified versions of this code in *any* form
 
  18 // |    that bears insufficient indications that the modifications are
 
  19 // |    not the work of the original author(s).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 define('INTERVAL_THIS_DAY', 1);
 
  30 define('INTERVAL_THIS_WEEK', 2);
 
  31 define('INTERVAL_THIS_MONTH', 3);
 
  32 define('INTERVAL_THIS_YEAR', 4);
 
  33 define('INTERVAL_ALL_TIME', 5);
 
  34 define('INTERVAL_LAST_WEEK', 6);
 
  35 define('INTERVAL_LAST_MONTH', 7);
 
  41         function Period($period_name=0, $date_point=null) {
 
  44                 if (!$date_point || !($date_point instanceof DateAndTime)) {
 
  45                         $date_point = new DateAndTime();
 
  47                 $startWeek = $user->week_start;
 
  49                 $date_begin = new DateAndTime();
 
  50                 $date_begin->setFormat($date_point->getFormat());
 
  51                 $date_end       = new DateAndTime();
 
  52                 $date_end->setFormat($date_point->getFormat());
 
  53                 $t_arr = localtime($date_point->getTimestamp());
 
  54                 $t_arr[5] = $t_arr[5] + 1900;
 
  56                 if ($t_arr[6] < $startWeek) {
 
  57                   $startWeekBias = $startWeek - 7;
 
  59                   $startWeekBias = $startWeek;
 
  62                 switch ($period_name) {
 
  63                         case INTERVAL_THIS_DAY:
 
  64                                 $date_begin->setTimestamp($date_point->getTimestamp());
 
  65                                 $date_end->setTimestamp($date_point->getTimestamp());
 
  67                         case INTERVAL_THIS_WEEK:
 
  68                           $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+$startWeekBias,$t_arr[5]));
 
  69                                 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+6+$startWeekBias,$t_arr[5]));
 
  71                         case INTERVAL_LAST_WEEK:
 
  72                                 $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-7+$startWeekBias,$t_arr[5]));
 
  73                                 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]-1+$startWeekBias,$t_arr[5]));
 
  75                         case INTERVAL_THIS_MONTH:
 
  76                                 $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4]+1,1,$t_arr[5]));
 
  77                                 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+2,0,$t_arr[5]));
 
  79                         case INTERVAL_LAST_MONTH:
 
  80                                 $date_begin->setTimestamp(mktime(0,0,0,$t_arr[4],1,$t_arr[5]));
 
  81                                 $date_end->setTimestamp(mktime(0,0,0,$t_arr[4]+1,0,$t_arr[5]));
 
  84                         case INTERVAL_THIS_YEAR:
 
  85                                 $date_begin->setTimestamp(mktime(0, 0, 0, 1, 1, $t_arr[5]));
 
  86                                 $date_end->setTimestamp(mktime(0, 0, 0, 12, 31, $t_arr[5]));
 
  89                 $this->mBeginDate       = &$date_begin;
 
  90                 $this->mEndDate         = &$date_end;
 
  94          * Return all days by period
 
  98         function getAllDays() {
 
 100                 if ($this->mBeginDate->before($this->mEndDate)) {
 
 101                         $d = $this->getBegin();
 
 102                         while ($d->before($this->getEnd())) {
 
 103                                 array_push($ret_array, $d);
 
 106                         array_push($ret_array, $d);
 
 108                         array_push($ret_array, $this->mBeginDate);
 
 113         function setPeriod($b_date, $e_date) {
 
 114                 $this->mBeginDate = $b_date;
 
 115                 $this->mEndDate = $e_date;
 
 118         // return date object
 
 119         function getBegin() {
 
 120                 return $this->mBeginDate;
 
 123         // return date object
 
 125                 return $this->mEndDate;
 
 128         // return date string
 
 129         function getBeginDate($format="") {
 
 130                 return $this->mBeginDate->toString($format);
 
 133         // return date string
 
 134         function getEndDate($format="") {
 
 135                 return $this->mEndDate->toString($format);
 
 138         function getArray($format="") {
 
 140                 $d = $this->getBegin();
 
 141                 while ($d->before($this->getEnd())) {
 
 142                         $result[] = $d->toString($format);