PHP时间类完整代码实例

admin3年前PHP教程89

开发中,经常用到时间的一些例子,比如昨天,今天,前天,近七天,一周等等。这里整理了一个时间的完整类实例,直接实例化,有需要的可以看看

以下直接代码


<?php
header("Content-type:text/html;Charset=utf-8");
class time{
 private $year;//年
 private $month;//月
 private $day;//天
 private $hour;//小时
 private $minute;//分钟
 private $second;//秒
 private $microtime;//毫秒
 private $weekday;//星期
 private $longDate;//完整的时间格式
 private $diffTime;//两个时间的差值
 //返回年份 time:时间格式为时间 2019-8-21
 function getyear($time="",$type=""){
 if($time==""){
 $time=time();
 }
 if($type==1){
 return $this->year=date("y",$time); //返回两位的年份 18
 }else{
 return $this->year=date("Y",$time); //返回四位的年份 2019
 }
 }
 //返回当前时间的月份 time:时间格式为时间 2019-8-21
 function getmonth($time="",$type=""){
 if($time==""){
 $time=time();
 }
 switch($type){
 case 1:$this->month=date("n",$time);//返回格式 8
  break;
 case 2:$this->month=date("m",$time);//返回格式 08
  break;
 case 3:$this->month=date("M",$time);//返回格式 Aug
  break;
 case 4:$this->month=date("F",$time);//返回格式 August
  break;
 default:$this->month=date("n",$time);
 }
 return $this->month;
 }
 //返回当前时间的天数 time:时间格式为时间 2019-8-21
 function getday($time="",$type=""){
 if($time==""){
 $time=time();
 }
 if($type==1){
 $this->day=date("d",$time);//返回格式 21
 }else{
 $this->day=date("j",$time);//返回格式 21
 }
 return $this->day;
 }
 //返回当前时间的小时 2019-08-21 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;
 }
 //返回当前时间的分钟数 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;
 }
 //比较两个时间的大小 格式 2019-8-21 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.'<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.'小时'.$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
  $time_var = "2019-08-21";
  $obj = new time();
  $year = $obj->getyear($time_var);
 
  echo($year);
?>

以上其他的方法也可以按照上面那个例子,输出你想要得到的日期,在开发过程中,可以直接放入在扩展库里,直接引用!

到此这篇关于PHP时间类完整代码实例的文章就介绍到这了,更多相关PHP时间类内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

gpu服务器是干什么的?租用香港GPU服务器如何选择?

近些年,人工智能可谓是水涨船高,成为科技进步不可或缺的一部分,也是大众口中的高端技术。特别是在图片、语音识别方面取得了卓越的成就,同时为席卷全球的新冠疫情也做出了巨大的贡献。人工智能能够在科技领域大显...

PHP解密支付宝小程序的加密数据、手机号的示例代码

1.小程序端代码示例my.getPhoneNumber({ success: (res) => { let encryptedData = res.response;&nbs...

普通的高防服务器如何增加防护?100G国内高防服务器如何防御DOSS攻击?

互联网的浪潮带动了服务器租赁市场的快速发展,许多的客户为了免往不必要的麻烦,都会选择访问距离相对较近、速度快而且不用备案的高防服务器。然而越来越大的市场也滋生了不少问题,比如日益严峻的网络流量攻击现象...

如何理解PHP核心特性命名空间

目录提出定义使用限定符在内部访问命名空间转义\符号提出PHP 在 5.3 后提出了命名空间用来解决组件之间的命名冲突问题,主要参考了文件系统的设计:同一个目录下不允许有相同的文件名 - 同一个命名空间...

东莞高防服务器哪家好怎么选择

东莞高防服务器哪家好怎么选择?选择东莞高防服务器时,建议注意以下几个方面来判断哪家好:服务商口碑和信誉:选择有良好口碑和信誉的服务商,可以通过搜索引擎、相关论坛和社交媒体等方式来了解。安全防护能力:选...

详解php微信小程序消息推送配置

第一步官网下载对应版本的cryptoDemo下载地址:wximg.gtimg/shake_tv/mpwiki/cryptoDemo.zip第二步创建检查文件wxcheck.php这个文件名可以随便命名...