<?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:时间格式为时间戳 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
}
}
//返回当前时间的月份 time:时间格式为时间戳 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;
}
//返回当前时间的天数 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 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;
}
//比较两个时间的大小 格式 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时间类完整实例的更多相关文章

  1. asp.net(C#)实现功能强大的时间日期处理类完整实例

    作者:smartsmile2012 字体:[增加 减小] 类型:转载 时间:2016-06-30我要评论 这篇文章主要介绍了asp.net(C#)实现功能强大的时间日期处理类,封装了针对日期与时间的各 ...

  2. (实用篇)PHP缓存类完整实例

    本文完整描述了一个简洁实用的PHP缓存类,可用来检查缓存文件是否在设置更新时间之内.清除缓存文件.根据当前动态文件生成缓存文件名.连续创建目录.缓存文件输出静态等功能.对于采用PHP开发CMS系统来说 ...

  3. php封装的mysqli类完整实例

    本文实例讲述了php封装的mysqli类.分享给大家供大家参考,具体如下:类: <?php header('content-type:text/html;charset=utf-8'); /* ...

  4. Java 线程安全LocalTime 和LocaldateTime 新的Date和Time类 -JDK8新时间类的简单使用

    不可变类且线程安全 LocalDate .java.time.LocalTime 和LocaldateTime  新的Date和Time类 DateTimeFormatter ==https://ww ...

  5. 前后端API交互数据加密——AES与RSA混合加密完整实例

    前言 前段时间看到一篇文章讲如何保证API调用时数据的安全性(传送门:https://blog.csdn.net/ityouknow/article/details/80603617),文中讲到利用R ...

  6. Netty实现SSL双向验证完整实例

    Netty实现SSL双向验证完整实例 博客分类: netty nettyssl自签证书  一.证书准备 要使用ssl双向验证,就必须先要生成服务端和客户端的证书,并相互添加信任,具体流程如下(本人调试 ...

  7. Java中六大时间类的使用和区别

    关于java中六个时间类的使用和区别 java.util.Date java.sql.Date  java.sql.Time  java.sql.Timestamp java.text.SimpleD ...

  8. [.net 面向对象编程基础] (9) 类和类的实例

    [.net 面向对象编程基础] (9) 类和类的实例 类 ,顾名思义就是分类.类别的意思.我们要面向对象编程,就需要对不同的事物进行分类.类可以说是.net面向对象的核心. 类:就是具有相同的属性和功 ...

  9. python基础——类和实例

    python基础——类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都 ...

随机推荐

  1. Exception in thread "main" java.util.InputMismatchException

    今天写代码来了一个异常 /** * 需求分析:根据输入的天数是否是周六或是周日, * 并且天气的温度大于28摄氏度,则外出游泳,否则钓鱼 * @author chenyanlong * 日期:2017 ...

  2. excel多元回归-系数参数解读

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  3. mac安装神器brew

    安装方法:命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...

  4. 函数和常用模块【day04】:作用域、局部和全局变量(四)

    本节内容 概述 课前前引 局部变量和全局变量 总结 一.概述 我们之前写代码,都需要声明变量,但是我们思考过变量的作用范围吗?今天我们就来讲讲变量的作用范围,这个作用范围又叫作用域.首先我们根据变量的 ...

  5. Swift学习笔记4

    1.延迟存储属性是指当第一次被调用的时候才会计算其初始值的属性.在属性声明前使用lazy来标示一个延迟存储属性. 必须将延迟存储属性声明成变量(使用var关键字),因为属性的初始值可能在实例构造完成之 ...

  6. __attribute__ 机制详解

    GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute).变量属性(Variable Attribute)和类型 ...

  7. synchronized的一些记录

    1.方法内的私有变量,不存在线程安全问题.非线程安全问题存在于实例变量(全局变量)中 2.在方法上加synchronized表示,当第一个线程进入时方法加锁(其他方法无法调用) 3.synchroni ...

  8. HDU - 3478 Catch(判奇环/二分图)

    http://acm.hdu.edu.cn/showproblem.php?pid=3478 题意 给一个无向图和小偷的起点,小偷每秒可以向相邻的点出发,问有没有一个时间点小偷可能出现在任何点. 分析 ...

  9. Git与GitHub学习笔记(四)合并远程分支

    在这里的前提: 1.你已经fork 源作者的项目到你自己的仓库了 2.git clone 自己仓库fork的项目,注意地址,这里是自己的账号下的地址,而不是源作者的项目地址哦 3.在本地修改代码,gi ...

  10. 在安卓上用Termux安装sqlmap

    1.打开Termux执行以下命令 apt update apt install git apt install python2 // 安装sqlmap运行环境 2.从github上下载sqlmap , ...