PHP时间类完整代码实例


Posted in PHP onFebruary 26, 2021

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

以下直接代码

<?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:时间格式为时间 2018-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); //返回四位的年份 2018
 }
 }
 //返回当前时间的月份 time:时间格式为时间 2018-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:时间格式为时间 2018-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;
 }
 //返回当前时间的小时 2018-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;
 }
 //比较两个时间的大小 格式 2018-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 = "2018-08-21";
  $obj = new time();
  $year = $obj->getyear($time_var);

  echo($year);
?>

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

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

PHP 相关文章推荐
以文本方式上传二进制文件的PHP程序
Oct 09 PHP
PHP4实际应用经验篇(7)
Oct 09 PHP
php file_exists 检查文件或目录是否存在的函数
May 10 PHP
PHP的几个常用数字判断函数代码
Apr 24 PHP
IIS6.0中配置php服务全过程解析
Aug 07 PHP
PHP制作3D扇形统计图以及对图片进行缩放操作实例
Oct 23 PHP
PHP封装的Twitter访问类实例
Jul 18 PHP
解决nginx不支持thinkphp中pathinfo的问题
Jul 21 PHP
PHP处理二进制数据的实现方法
Jun 13 PHP
PHP中include和require的区别实例分析
May 07 PHP
PHP读取并输出XML文件数据的简单实现方法
Dec 22 PHP
php引用和拷贝的区别知识点总结
Sep 23 PHP
PHP队列场景以及实现代码实例详解
Feb 26 #PHP
PHP实现长轮询消息实时推送功能代码实例讲解
Feb 26 #PHP
php的对象传值与引用传值代码实例讲解
Feb 26 #PHP
php并发加锁问题分析与设计代码实例讲解
Feb 26 #PHP
PHP内存溢出优化代码详解
Feb 26 #PHP
php自动加载代码实例详解
Feb 26 #PHP
PHP的重载使用魔术方法代码实例详解
Feb 26 #PHP
You might like
php 图片上添加透明度渐变的效果
2009/06/29 PHP
解析php获取字符串的编码格式的方法(函数)
2013/06/21 PHP
destoon找回管理员密码的方法
2014/06/21 PHP
Laravel 4 初级教程之视图、命名空间、路由
2014/10/30 PHP
PDO预处理语句PDOStatement对象使用总结
2014/11/20 PHP
深入理解Yii2.0乐观锁与悲观锁的原理与使用
2017/07/26 PHP
PHP常用函数之获取汉字首字母功能示例
2019/10/21 PHP
Laravel框架Blade模板简介及模板继承用法分析
2019/12/03 PHP
通用于ie和firefox的函数 GetCurrentStyle (obj, prop)
2006/12/27 Javascript
js读写(删除)Cookie实例详解
2013/04/17 Javascript
AngularJS入门教程之ng-class 指令用法
2016/08/01 Javascript
vue.js指令v-for使用及索引获取
2016/11/03 Javascript
JavaScript cookie详解及简单实例应用
2016/12/31 Javascript
bootstrap实现动态进度条效果
2017/03/08 Javascript
JS内置对象和Math对象知识点详解
2020/04/03 Javascript
Python base64编码解码实例
2015/06/21 Python
python删除过期log文件操作实例解析
2018/01/31 Python
使用numpy和PIL进行简单的图像处理方法
2018/07/02 Python
python 用下标截取字符串的实例
2018/12/25 Python
学生信息管理系统Python面向对象版
2019/01/30 Python
详解Python中pandas的安装操作说明(傻瓜版)
2019/04/08 Python
python中openpyxl和xlsxwriter对Excel的操作方法
2021/03/01 Python
CSS3贝塞尔曲线示例:创建链接悬停动画效果
2020/11/19 HTML / CSS
英国设计的甲板鞋和船鞋:Chatham
2018/12/06 全球购物
Nebula美国官网:便携式投影仪
2019/03/15 全球购物
世界上最大的字体市场:MyFonts
2020/01/10 全球购物
小学班干部竞选演讲稿
2014/04/24 职场文书
禁止高声喧哗的标语
2014/06/11 职场文书
党员民主生活会材料
2014/12/15 职场文书
街道党风廉政建设调研报告
2015/01/01 职场文书
为什么中国式养孩子很累?
2019/08/07 职场文书
解读Vue组件注册方式
2021/05/15 Vue.js
react中的DOM操作实现
2021/06/30 Javascript
MySQL数据库10秒内插入百万条数据的实现
2021/11/01 MySQL
安装Windows Server 2012 R2企业版操作系统并设置好相关参数
2022/04/29 Servers
Android Studio实现简易进制转换计算器
2022/05/20 Java/Android