From 5176f5d77f6b541776dcaa11f06e11fc19d70d89 Mon Sep 17 00:00:00 2001 From: zhujinyong Date: Mon, 7 Jan 2013 05:50:18 +0000 Subject: [PATCH] + add date class. --- lib/date/date.class.php | 249 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 lib/date/date.class.php diff --git a/lib/date/date.class.php b/lib/date/date.class.php new file mode 100644 index 0000000000..e45e5f653c --- /dev/null +++ b/lib/date/date.class.php @@ -0,0 +1,249 @@ + 60 - $delta) + { + $hour += 1; + $minute = 00; + } + else + { + for($i = 0; $i < $delta; $i ++) + { + if(in_array($minute + $i, $range)) + { + $minute = $minute + $i; + break; + } + } + } + + return sprintf('%02d%02d', $hour, $minute); + } + + /** + * Format time 0915 to 09:15 + * + * @param string $time + * @access public + * @return string + */ + public static function formatTime($time) + { + if(strlen($time) != 4 or $time == '2400') return ''; + return substr($time, 0, 2) . ':' . substr($time, 2, 2); + } + + /** + * Get the begin and end date of this week. + * + * @access public + * @return array + */ + public static function getThisWeek() + { + $baseTime = self::getMiddleOfThisWeek(); + $begin = date(DT_DATE1, strtotime('last monday', $baseTime)) . ' 00:00:00'; + $end = date(DT_DATE1, strtotime('next sunday', $baseTime)) . ' 23:59:59'; + return array('begin' => $begin, 'end' => $end); + } + + /** + * Get the begin and end date of last week. + * + * @access public + * @return array + */ + public static function getLastWeek() + { + $baseTime = self::getMiddleOfLastWeek(); + $begin = date(DT_DATE1, strtotime('last monday', $baseTime)) . ' 00:00:00'; + $end = date(DT_DATE1, strtotime('next sunday', $baseTime)) . ' 23:59:59'; + return array('begin' => $begin, 'end' => $end); + } + + /** + * Get the time at the middle of this week. + * + * If today in week is 1, move it one day in future. Else is 7, move it back one day. To keep the time geted in this week. + * + * @access public + * @return time + */ + public static function getMiddleOfThisWeek() + { + $baseTime = time(); + $weekDay = date('N'); + if($weekDay == 1) $baseTime = time() + 86400; + if($weekDay == 7) $baseTime = time() - 86400; + return $baseTime; + } + + /** + * Get middle of last week + * + * @access public + * @return time + */ + public static function getMiddleOfLastWeek() + { + $baseTime = time(); + $weekDay = date('N'); + $baseTime = time() - 86400 * 7; + if($weekDay == 1) $baseTime = time() - 86400 * 4; // Make sure is last thursday. + if($weekDay == 7) $baseTime = time() - 86400 * 10; // Make sure is last thursday. + return $baseTime; + } + + /** + * Get this month begin and end time + * + * @access public + * @return array + */ + public static function getThisMonth() + { + $begin = date('Y-m') . '-01 00:00:00'; + $end = date('Y-m', strtotime('next month')) . '-00 23:59:59'; + return array('begin' => $begin, 'end' => $end); + } + + /** + * Get last month begin and end time + * + * @access public + * @return array + */ + public static function getLastMonth() + { + $begin = date('Y-m', strtotime('last month')) . '-01 00:00:00'; + $end = date('Y-m', strtotime('this month')) . '-00 23:59:59'; + return array('begin' => $begin, 'end' => $end); + } + + public static function getThisSeason() + { + $year = date("Y-"); + $month = date("n"); + if($month % 3) + { + $seasonBegin = $month - ($month % 3) + 1; + $seasonEnd = $seasonBegin + 3; + } + else + { + $seasonEnd = $month + 1; + $seasonBegin = $seasonEnd - 3; + } + + if(strlen($seasonBegin) == 1) $seasonBegin = "0$seasonBegin"; + if(strlen($seasonEnd) == 1) $seasonEnd = "0$seasonEnd"; + $begin = $year . $seasonBegin; + $end = $year . $seasonEnd; + + return array('begin' => $begin, 'end' => $end); + } + + public static function getThisYear() + { + $begin = date(DT_DATE1, strtotime('1/1 this year')); + $end = date(DT_DATE1, strtotime('1/1 next year -1 day')); + return array('begin' => $begin, 'end' => $end); + } +}