您的位置:首页 > 编程语言 > PHP开发

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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: