PHP時(shí)間類完整實(shí)例(非常實(shí)用)
更新時(shí)間:2015年12月25日 12:21:40 作者:釋然me
這篇文章主要介紹了PHP時(shí)間類完整實(shí)例,涉及PHP針對日期、時(shí)間、星期等的獲取與比較等操作技巧,非常簡單實(shí)用,需要的朋友可以參考下
本文實(shí)例講述了PHP時(shí)間類。分享給大家供大家參考,具體如下:
<?php
header("Content-type:text/html;Charset=utf-8");
class time{
private $year;//年
private $month;//月
private $day;//天
private $hour;//小時(shí)
private $minute;//分鐘
private $second;//秒
private $microtime;//毫秒
private $weekday;//星期
private $longDate;//完整的時(shí)間格式
private $diffTime;//兩個時(shí)間的差值
//返回年份 time:時(shí)間格式為時(shí)間戳 2013-3-27
function getyear($time="",$type=""){
if($time==""){
$time=time();
}
if($type==1){
return $this->year=date("y",$time); //返回兩位的年份 13
}else{
return $this->year=date("Y",$time); //返回四位的年份 2013
}
}
//返回當(dāng)前時(shí)間的月份 time:時(shí)間格式為時(shí)間戳 2013-3-27
function getmonth($time="",$type=""){
if($time==""){
$time=time();
}
switch($type){
case 1:$this->month=date("n",$time);//返回格式 3
break;
case 2:$this->month=date("m",$time);//返回格式 03
break;
case 3:$this->month=date("M",$time);//返回格式 Mar
break;
case 4:$this->month=date("F",$time);//返回格式 March
break;
default:$this->month=date("n",$time);
}
return $this->month;
}
//返回當(dāng)前時(shí)間的天數(shù) time:時(shí)間格式為時(shí)間戳 2013-3-4
function getday($time="",$type=""){
if($time==""){
$time=time();
}
if($type==1){
$this->day=date("d",$time);//返回格式 04
}else{
$this->day=date("j",$time);//返回格式 4
}
return $this->day;
}
//返回當(dāng)前時(shí)間的小時(shí) 2010-11-10 1:19:21 20:19:21
function gethour($time="",$type=""){
if($time==""){
$time=time();
}
switch($type){
case 1:$this->hour=date("H",$time);//格式: 1 20
break;
case 2:$this->hour=date("h",$time);//格式 01 08
break;
case 3:$this->hour=date("G",$time);//格式 1 20
break;
case 4:$this->hour=date("g",$time);//格式 1 8
break;
default :$this->hour=date("H",$time);
}
return $this->hour;
}
//返回當(dāng)前時(shí)間的分鐘數(shù) 1:9:18
function getminute($time="",$type=""){
if($time==""){
$time=time();
}
$this->minute=date("i",$time); //格式 09
return $this->minute;
}
//返回當(dāng)前時(shí)間的秒數(shù) 20:19:01
function getsecond($time="",$type=""){
if($time==""){
$time=time();
}
$this->second=date("s",$time); //格式 01
return $this->second;
}
//返回當(dāng)前時(shí)間的星期數(shù)
function getweekday($time="",$type=""){
if($time==""){
$time=time();
}
if($type==1){
$this->weekday=date("D",$time);//格式 Sun
}else if($type==2){
$this->weekday=date("l",$time); //格式 Sunday
}else{
$this->weekday=date("w",$time);//格式 數(shù)字表示 0--6
}
return $this->weekday;
}
//比較兩個時(shí)間的大小 格式 2013-3-4 8:4:3
function compare($time1,$time2){
$time1=strtotime($time1);
$time2=strtotime($time2);
if($time1>=$time2){ //第一個時(shí)間大于等于第二個時(shí)間 返回1 否則返回0
return 1;
}else{
return -1;
}
}
//比較兩個時(shí)間的差值
function diffdate($time1="",$time2=""){
//echo $time1.'------'.$time2.'<br>';
if($time1==""){
$time1=date("Y-m-d H:i:s");
}
if($time2==""){
$time2=date("Y-m-d H:i:s");
}
$date1=strtotime($time1);
$date2=strtotime($time2);
if($date1>$date2){
$diff=$date1-$date2;
}else{
$diff=$date2-$date1;
}
if($diff>=0){
$day=floor($diff/86400);
$hour=floor(($diff%86400)/3600);
$minute=floor(($diff%3600)/60);
$second=floor(($diff%60));
$this->diffTime='相差'.$day.'天'.$hour.'小時(shí)'.$minute.'分鐘'.$second.'秒';
}
return $this->diffTime;
}
//返回 X年X月X日
function buildDate($time="",$type=""){
if($type==1){
$this->longDate = $this->getyear($time) . '年' . $this->getmonth($time) . '月' . $this->getday($time) . '日';
}else{
$this->longDate = $this->getyear($time) . '年' . $this->getmonth($time) . '月' . $this->getday($time) . '日'.$this->gethour($time).':'.$this->getminute($time).':'.$this->getsecond($time);
}
return $this->longDate;
}
}
?>
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
相關(guān)文章
WordPress遷移時(shí)一些常見問題的解決方法整理
這篇文章主要介紹了WordPress遷移時(shí)一些常見問題的解決方法整理,包括通過一個推薦的方法來備份插件以避免遷移后的更多問題出現(xiàn),需要的朋友可以參考下2015-11-11
PHP實(shí)現(xiàn)PDO的mysql數(shù)據(jù)庫操作類
這篇文章主要介紹了PHP實(shí)現(xiàn)PDO的mysql數(shù)據(jù)庫操作類,其中dbconfig類負(fù)責(zé)配置數(shù)據(jù)庫訪問信息,dbtemplate類集合了對數(shù)據(jù)庫的訪問操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
解析WordPress中控制用戶登陸和判斷用戶登陸的PHP函數(shù)
這篇文章主要介紹了WordPress中控制用戶登陸和判斷用戶登陸的PHP函數(shù),WordPress現(xiàn)在多用戶功能已推出了一段時(shí)間,針對多用戶需求的開發(fā)也日益增多,需要的朋友可以參考下2016-03-03
PHP中使用SimpleXML檢查XML文件結(jié)構(gòu)實(shí)例
這篇文章主要介紹了PHP中使用SimpleXML檢查XML文件結(jié)構(gòu)實(shí)例,本文講解使用SimpleXML來檢查一個XML文件是否符合規(guī)范的方法,需要的朋友可以參考下2015-01-01
PHP實(shí)現(xiàn)JS中escape與unescape的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)JS中escape與unescape的方法,通過json_encode和json_decode方法實(shí)現(xiàn)JS中escape與unescape函數(shù)的功能,需要的朋友可以參考下2016-07-07
php數(shù)組合并array_merge()函數(shù)使用注意事項(xiàng)
array_merge()函數(shù)在php中是對數(shù)組進(jìn)行合并的,可以把多個數(shù)組合成一個數(shù)組,并且不改變原數(shù)組(www.111cn.net)的值了,但今天我在使用array_merge合并數(shù)組時(shí)碰到幾個小細(xì)節(jié)上的問題,下面我舉例子給各位朋友看看2014-06-06

