php--常用的时间处理函数
2014-10-19 00:19
639 查看
天地四方曰宇,往古来今曰宙
时间是世界的重要组成部分,不论花开花落,还是云卷云舒都有它的影子。
但它源起何处?又将去向何方?没人知道答案,也不需要答案,我们需要的只是一个相对的起点来标识时间,现今世界普遍采用公元纪年法来表示。
公元纪年法以耶稣诞生日记为公元1年(没有公元0年),中国处于汉平帝刘衎(不会读。。。)登基第二年即元始元年。
关于时间的另一个概念是unix时间戳,是从1970年1月1日开始所经过的秒数,不考虑闰秒,什么是闰秒参考这里。
下面就来说说php中时间的处理方法,以获取当前时间为例
<?php date_default_timezone_set('Asia/Shanghai'); echo "now is ".date("Y-m-d H:i:s",time())."\n"; echo "now is ".date("Y-m-d H:i:s",strtotime("now"))."\n"; $date = new DateTime(); echo "now is ".$date->format("Y-m-d H:i:s")."\n"; ?>
<?php /** * process date * @author huntstack * @time 2014-10-18 */ class Process_Date{ private $date; private $M,$D,$Y; /** * set the date for next operator * @parameter date:time string or timestamp */ function __construct($date=""){ if($date == "" || empty($date)){ $this->date = strtotime("now"); }else if(gettype($date) == "string"){ $this->date = strtotime($date); }else if(gettype($date) == "integer"){ $this->date = $date; }else{ throw new Exception("paramter must be timestamp or date string or empty for current time"); } $this->set_varibales(); } public function set_date($date){ $this->date = strtotime($date); $this->set_varibales(); } private function set_varibales(){ $this->M = date("m",$this->date); $this->D = date("d",$this->date); $this->Y = date("Y",$this->date); } /** * get the specified weeks * @parameter $i:numbers of week * @parameter $flag:0->last,1->next */ public function get_week($i=0,$flag=1){ if($flag == 0) return date("YW",strtotime("-$i week",$this->date)); else if($flag == 1) return date("YW",strtotime("+$i week",$this->date)); } /** * get the specified months * @parameter $i:numbers of month * @parameter $flag:0->last,1->next */ public function get_month($i=0,$flag=1){ if($flag == 0) return date("Y-m",mktime(0,0,0, $this->M-$i, 1, $this->Y)); else if($flag == 1) return date("Y-m",mktime(0,0,0, $this->M+$i, 1, $this->Y)); } /** * get the specified days * @parameter $i:numbers of day * @parameter $flag:0->last,1->next */ public function get_day($i=0,$flag=1){ if($flag == 0) return date("Y-m-d",mktime(0,0,0, $this->M, $this->D-$i, $this->Y)); else if($flag == 1) return date("Y-m-d",mktime(0,0,0, $this->M, $this->D+$i, $this->Y)); } /** * get the last $count days * @parameter count:number */ public function get_last_days($count){ $return = array(); for($i=1;$i<=$count;$i++){ array_push($return, $this->get_day($i,0)); } return $return; } /** * get the next $count days * @parameter count:number */ public function get_next_days($count){ $return = array(); for($i=1;$i<=$count;$i++){ array_push($return, $this->get_day($i,1)); } return $return; } /** * get the last $count weeks * @parameter count:number */ public function get_last_weeks($count){ $return = array(); for($i=1;$i<=$count;$i++){ array_push($return, $this->get_week($i,0)); } return $return; } /** * get the next $count weeks * @parameter count:number */ public function get_next_weeks($count){ $return = array(); for($i=1;$i<=$count;$i++){ array_push($return, $this->get_week($i,1)); } return $return; } /** * get the last $count months * @parameter count:number */ public function get_last_month($count){ $return = array(); for($i=1;$i<=$count;$i++){ array_push($return, $this->get_month($i,0)); } return $return; } /** * get the next $count months * @parameter count:number */ public function get_next_month($count){ $return = array(); for($i=1;$i<=$count;$i++){ array_push($return, $this->get_month($i,1)); } return $return; } /** * get the first day and the last day of a week */ public function get_week_begin_end(){ $return["begin"] = mktime(0,0,0, $this->M, $this->D-date("w",$this->date)+1, $this->Y); $return["end"] = mktime(23,59,59, $this->M, $this->D-date("w",$this->date)+7, $this->Y); return $return; } /** * get the first day and the last day of a month */ public function get_month_begin_end(){ $return["begin"] = strtotime("first day of",$this->date); $return["end"] = strtotime("last day of",$this->date); return $return; } } ?>
View Code
参考资料:
1.http://php.net/manual/zh/book.datetime.php
2.http://www.phppan.com/2011/06/php-strtotime/
相关文章推荐
- PHP date函数常用时间处理方法
- php 常用日期处理函数,时间戳处理函数等,如下讲解
- PHP date函数常用时间处理方法
- PHP - strtotime time FROM_UNIXTIME UNIX_TIMESTAMP 常用的时间&时间戳处理函数
- php 时间处理常用函数
- 仿php的日期函数,asp时间处理函数
- php时间处理函数
- PHP常用时间函数
- PHP 常用时间函数
- PHP时间处理函数
- php时间处理函数
- php时间处理函数
- Oracle常用的一些时间处理函数
- 常用的php文件处理函数
- 常用的php字符串处理函数
- 总结的php10个常用的处理字符串的函数事例
- SQL常用日期时间处理函数
- php 时间处理函数--获取某个间隔前的日期
- PHP 常用的时间函数,session、 cookies使用
- php中一些常用且好用的字符处理函数(一)