Dates和 Time的OOP实现

Stella981
• 阅读 941

Date/Time的PHP扩展从PHP 5.2开始就默认被支持,所有这些都被用于现实场景:
1.date 或者 time被DateTime 对象所取代;
2.timezone被DateTimeZone 对象取代;
3.DateInterval 对象代表一个时间间隔,例如,我们说距离现在2天以后,“2天”就是间隔,DateInterval对象不代表一个具体的时间;
4.DatePeriod代表两天之间的时间;

从date()到DateTime
以前我们显示时间使用 date(),他工作简单,我们只需要填入一个日期格式,但痛苦的是更具不同时区来格式化输出时间。

DateTime 就比date支持更多的格式,在我们使用之前需要新建一个DateTime 对象:

$date = new DateTime();

DateTime的析构函数默认是当前时间“now”,我们可以根据自身需要输入一个自己需要的时间字符串,下面我们例了一些demo:

new DateTime('2013, March 24') //DateTime representing 2013, March 24
new DateTime('2013-03-24') //DateTime representing 2013, March 24
new DateTime('+2 days') //DateTime representing 2 days from now on.
new DateTime('tomorrow')

在这里你可以查看PHP默认支持的时间格式:http://www.php.net/manual/en/datetime.formats.php,当然如果没有,你可以自己通过DateTime::createFromFormat方法来自定义创建日期

DateTime::createFromFormat('j-M-Y', '24-Mar-2013');

现在看上去DateTime真是非常的简单。

Unix时间戳
$date->getTimestamp(); //返回一个unix时间戳

修改Date/Times
$date->setDate(2013, 12, 30); //yyyy, mm, dd will set the the specified date
$date->setTime(12, 3, 20); //hh, mm, ss (optional) will modify the time
$date->modify('tomorrow'); //string based manipulation
$date->setTimestamp(1364798550); //modify using a unix timestamp

用多个Dates来操作
现在我们痴迷于DateTime,接下来我们来学习更多的东西,这里有个例子就是比较两个生日大小:

$sheldon = new DateTime('May 20th, 1980');
$neo = new DateTime('March 11th, 1962');

if ($sheldon > $neo)
echo 'Sheldon is younger than neo';

我们用TestPHP来看下运行结果(如图1)
Dates和 Time的OOP实现

当然我们可以对比两个时间的不同,

$diff = $neo->diff($sheldon);

我们来看下调用DateInterval对象的diff 方法返回的数据(如图2)
Dates和 Time的OOP实现

当然还可以输出友好格式,例如:

$diff->format('Neo is older by %Y years and %m months older');

看下输出结果(如图3)
Dates和 Time的OOP实现

如果我们给neo增加这个时间差,就会将他的生日改变成sheldon的了,我们可以这样操作:

$neo->add($diff); //neo's birthday changed to sheldon's

diff不是只能在生成DateInterval 对象的时候的唯一地方,创建新对象可以像这样:

$new_diff = new DateInterval('P2Y');

这里有一些析构函数的格式http://www.php.net/manual/en/dateinterval.construct.php

好了,我们今天就先了解到这里,下一篇我们再继续了解。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
3年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
Wesley13 Wesley13
2年前
java常用类(2)
三、时间处理相关类Date类:计算机世界把1970年1月1号定为基准时间,每个度量单位是毫秒(1秒的千分之一),用long类型的变量表示时间。Date分配Date对象并初始化对象,以表示自从标准基准时间(称为“历元”(epoch),即1970年1月1日08:00:00GMT)以来的指定毫秒数。示例:packagecn.tanjian
待兔 待兔
2个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Stella981 Stella981
2年前
Django之Django模板
1、问:html页面从数据库中读出DateTimeField字段时,显示的时间格式和数据库中存放的格式不一致,比如数据库字段内容为2012082616:00:00,但是页面显示的却是Aug.26,2012,4p.m.答:为了页面和数据库中显示一致,需要在页面格式化时间,需要添加<td{{dayrecord.p\_time|date:
Stella981 Stella981
2年前
Python之time模块的时间戳、时间字符串格式化与转换
Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
Python time模块 返回格式化时间
常用命令  strftimetime.strftime("%Y%m%d%H:%M:%S",formattime)第二个参数为可选参数,不填第二个参数则返回格式化后的当前时间日期201812112:00:00time.strftime('%H:%M:%S')返回当前时间的时分秒time.strftim
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_