找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 241|回复: 0
打印 上一主题 下一主题

PHP时间类完整实例(非常实用)

[复制链接]

2588

主题

2588

帖子

7694

积分

论坛元老

Rank: 8Rank: 8

积分
7694
跳转到指定楼层
楼主
发表于 2018-2-14 05:40:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了PHP时间类。分享给大家供大家参考,具体如下:
year=date("y",$time); //返回两位的年份 13
}else{
  return $this->year=date("Y",$time); //返回四位的年份 2013
}
}
//返回当前时间的月份 time:时间格式为时间戳 2013-3-27
function getmonth($time="",$type=""){
if($time==""){
  $time=time();
}
switch($type){
  case 1this->month=date("n",$time);//返回格式 3
   break;
  case 2this->month=date("m",$time);//返回格式 03
   break;
  case 3this->month=date("M",$time);//返回格式 Mar
   break;
  case 4this->month=date("F",$time);//返回格式 March
   break;
  defaultthis->month=date("n",$time);
}
return $this->month;
}
//返回当前时间的天数 time:时间格式为时间戳 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;
}
//返回当前时间的小时  2010-11-10 1:19:21 20:19:21
function gethour($time="",$type=""){
if($time==""){
  $time=time();
}
switch($type){
  case 1this->hour=date("H",$time);//格式: 1 20
   break;
  case 2this->hour=date("h",$time);//格式  01 08
   break;
  case 3this->hour=date("G",$time);//格式  1 20
   break;
  case 4this->hour=date("g",$time);//格式  1 8
   break;
  default this->hour=date("H",$time);
}
return $this->hour;
}
//返回当前时间的分钟数 1:9:18  
function getminute($time="",$type=""){
if($time==""){
  $time=time();
}
$this->minute=date("i",$time); //格式  09
return $this->minute;
}
//返回当前时间的秒数  20:19:01
function getsecond($time="",$type=""){
if($time==""){
  $time=time();
}
$this->second=date("s",$time); //格式  01
return $this->second;
}
//返回当前时间的星期数
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);//格式 数字表示 0--6
}
return $this->weekday;
}
//比较两个时间的大小 格式 2013-3-4 8:4:3  
function compare($time1,$time2){
$time1=strtotime($time1);
$time2=strtotime($time2);
if($time1>=$time2){  //第一个时间大于等于第二个时间 返回1 否则返回0
  return 1;
}else{
  return -1;
}
}
//比较两个时间的差值
function diffdate($time1="",$time2=""){
//echo $time1.'------'.$time2.'
';
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.'小时'.$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程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • PHP 将图片按创建时间进行分类存储的实现代码
  • PHP 一个页面执行时间类代码
  • php代码运行时间查看类代码分享
  • PHP性能优化工具篇Benchmark类调试执行时间
  • PHP测试程序运行时间的类
  • php中计算程序运行时间的类代码
  • php实现的Timer页面运行时间监测类
  • php实现用于计算执行时间的类实例
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端