欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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;//兩個(gè)時(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;
 }
 //比較兩個(gè)時(shí)間的大小 格式 2013-3-4 8:4:3  
 function compare($time1,$time2){
 $time1=strtotime($time1);
 $time2=strtotime($time2);
 if($time1>=$time2){  //第一個(gè)時(shí)間大于等于第二個(gè)時(shí)間 返回1 否則返回0
  return 1;
 }else{
  return -1;
 }
 }
 //比較兩個(gè)時(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)文章

  • PHP數(shù)組操作匯總 php數(shù)組的使用技巧

    PHP數(shù)組操作匯總 php數(shù)組的使用技巧

    對于Web編程來說,最重要的就是存取和讀寫數(shù)據(jù)了。存儲方式可能有很多種,可以是字符串、數(shù)組、文件的形式等。
    2011-07-07
  • PHP開發(fā)中常用的字符串操作函數(shù)

    PHP開發(fā)中常用的字符串操作函數(shù)

    在編程的過程當(dāng)中,字符串的操作是非常重要的并且經(jīng)常會被用到,字符串常用的操作具體的包括字符串的拼接,替換字符串,查找字符串,比較字符串,復(fù)制字符串以及計(jì)算字符串的長度等等.
    2011-02-02
  • php實(shí)現(xiàn)猴子選大王問題算法實(shí)例

    php實(shí)現(xiàn)猴子選大王問題算法實(shí)例

    這篇文章主要介紹了php實(shí)現(xiàn)猴子選大王問題算法,實(shí)例分析了算法的原理與解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • 遞歸實(shí)現(xiàn)php數(shù)組轉(zhuǎn)xml的代碼分享

    遞歸實(shí)現(xiàn)php數(shù)組轉(zhuǎn)xml的代碼分享

    本文以實(shí)例形式講述了PHP實(shí)現(xiàn)數(shù)組遞歸轉(zhuǎn)義的方法,分享給大家供大家參考之用。具體方法如下:
    2015-05-05
  • PHP函數(shù)超時(shí)處理方法

    PHP函數(shù)超時(shí)處理方法

    這篇文章主要介紹了PHP函數(shù)超時(shí)處理方法,結(jié)合實(shí)例形式分析了基于register_shutdown_function的超時(shí)處理相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-02-02
  • php數(shù)組函數(shù)序列之a(chǎn)rray_pop() - 刪除數(shù)組中的最后一個(gè)元素

    php數(shù)組函數(shù)序列之a(chǎn)rray_pop() - 刪除數(shù)組中的最后一個(gè)元素

    定義和用法array_pop() 函數(shù)刪除數(shù)組中的最后一個(gè)元素。
    2011-11-11
  • 使用bcompiler對PHP文件進(jìn)行加密的代碼

    使用bcompiler對PHP文件進(jìn)行加密的代碼

    在網(wǎng)上無意間看到這個(gè)功能代碼,還沒有去試,以后有機(jī)會用到時(shí)在試一試。收藏一下。
    2010-08-08
  • php超詳細(xì)講解命名管道

    php超詳細(xì)講解命名管道

    這篇文章主要介紹了php中通過命名管道實(shí)現(xiàn)跨語言進(jìn)程間通信的案例,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下
    2022-07-07
  • 基于PHP做個(gè)圖片防盜鏈

    基于PHP做個(gè)圖片防盜鏈

    本文主要介紹了Referer原理與圖片防盜鏈實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Referer頭信息原理與圖片防盜鏈判定、實(shí)現(xiàn)方法,并附帶一個(gè)Http請求封裝類,需要的朋友可以參考下
    2022-12-12
  • php filter協(xié)議使用方法

    php filter協(xié)議使用方法

    php://filter是一種設(shè)計(jì)用來允許過濾器程序在打開時(shí)成為流的封裝協(xié)議。這對于單獨(dú)具有完整功能的文件函數(shù)非常有用,否則就沒有機(jī)會在讀取內(nèi)容之前將過濾器應(yīng)用于流之上
    2022-12-12

最新評論