
wangbin_yg LV10
2017-01-07
满意答案
nhhxr
LV9
2017-01-08
PHP 中的 strtotime() 函数可以实现
strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。
strtotime(time,now)
time 规定要解析的时间字符串。
now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。
成功则返回时间戳,否则返回 FALSE。在 PHP 510 之前本函数在失败时返回 -1
你要先搞清楚时间戳是什么!
在PHP中,时间戳是1970年1月1日0时0分到一个指定的时间点的秒数。5分钟是一个时间区间,不是一个时间点,所以不能转换为时间戳。除非把5分钟限定成一个时间点概念,例如:距离当前时间5分钟之前,距离1970年1月1日0时0分的5分钟之后。
搞清之后就是代码了,PHP中有两个可以生成时间戳的函数,mktime()和strtotime()
PHP获取开始和结束时间
//当前时间
$start
=
strtotime(date('Y-m-d
H:i:s'));
//时长,时间长度(秒为单位,例子中为120秒,2分钟后,实际时间可自行修改或程序计算得出)
//如果是1周后,则为$start
+
(7
24
60
60);
$long
=
$start
+
120
//结束时间
$end
=
date('Y-m-d
H:i:s',
$long);
php可以用函数time()来获取Unix
时间戳,但是只能获取当前的,不能填入参数计算
<php
/
author: china_skag
time: 2014-07-08
发博时间计算(年,月,日,时,分,秒)
$createtime 可以是当前时间
$gettime 你要传进来的时间
/
class Mygettime{
function __construct($createtime,$gettime) {
$this->createtime = $createtime;
$this->gettime = $gettime;
}
function getSeconds()
{
return $this->createtime-$this->gettime;
}
function getMinutes()
{
return ($this->createtime-$this->gettime)/(60);
}
function getHours()
{
return ($this->createtime-$this->gettime)/(6060);
}
function getDay()
{
return ($this->createtime-$this->gettime)/(606024);
}
function getMonth()
{
return ($this->createtime-$this->gettime)/(60602430);
}
function getYear()
{
return ($this->createtime-$this->gettime)/(6060243012);
}
function index()
{
if($this->getYear() > 1)
{
if($this->getYear() > 2)
{
return date("Y-m-d",$this->gettime);
exit();
}
return intval($this->getYear())" 年前";
exit();
}
if($this->getMonth() > 1)
{
return intval($this->getMonth())" 月前";
exit();
}
if($this->getDay() > 1)
{
return intval($this->getDay())" 天前";
exit();
}
if($this->getHours() > 1)
{
return intval($this->getHours())" 小时前";
exit();
}
if($this->getMinutes() > 1)
{
return intval($this->getMinutes())" 分钟前";
exit();
}
if($this->getSeconds() > 1)
{
return intval($this->getSeconds()-1)" 秒前";
exit();
}
}
}
//类的使用实例
/
调用类输出方式
$a = new Mygettime(time(),strtotime('-25 month'));
echo iconv('utf-8', 'gb2312', $a->index())iconv('utf-8', 'gb2312', $a->index()):iconv('utf-8', 'gb2312', '当前');
/
以上就是关于php 如何把5分钟转化为时间戳全部的内容,包括:php 如何把5分钟转化为时间戳、PHP怎么获得一天,一周,一个月的起始和结束的时间戳求高人指点、php当前时间夹30分钟等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)