这篇文章主要介绍了php计算时间几分钟前、几小时前、几天前的几个函数、类分享,需要的朋友可以参考下
一、函数实现
实例1:

<?php
header("Content-type: text/html; charset=utf8");
date_default_timezone_set("Asia/Shanghai"); //设置时区
function time_tran($the_time) {
$now_time = date("Y-m-d H:i:s", time());
//echo $now_time;
$now_time = strtotime($now_time);
$show_time = strtotime($the_time);
$dur = $now_time - $show_time;
if ($dur < 0) {
return $the_time;
} else {
if ($dur < 60) {
return $dur . '秒前';
} else {
if ($dur < 3600) {
return floor($dur / 60) . '分钟前';
} else {
if ($dur < 86400) {
return floor($dur / 3600) . '小时前';
} else {
if ($dur < 259200) {//3天内
return floor($dur / 86400) . '天前';
} else {
return $the_time;
}
}
}
}
}
} echo time_tran("2014-7-8 19:22:01");
?>

实例2:

<?php
function time_tranx($the_time){
$now_time = date("Y-m-d H:i:s",time()+8*60*60);
$now_time = strtotime($now_time);
$show_time = strtotime($the_time);
$dur = $now_time - $show_time;
if($dur < 0){
return $the_time;
}else{
if($dur < 60){
return $dur.'秒前';
}else{
if($dur < 3600){
return floor($dur/60).'分钟前';
}else{
if($dur < 86400){
return floor($dur/3600).'小时前';
}else{
if($dur < 259200){ //3天内
return floor($dur/86400).'天前';
}else{
return $the_time;
}
}
}
}
}
}
echo time_tranx("2014-7-8 19:22:01");
?>

实例3:

<?php
function format_date($time){
$t=time()-$time;
<span style="white-space:pre"> </span>//echo time();
$f=array(
'31536000'=>'年',
'2592000'=>'个月',
'604800'=>'星期',
'86400'=>'天',
'3600'=>'小时',
'60'=>'分钟',
'1'=>'秒'
);
foreach ($f as $k=>$v) {
if (0 !=$c=floor($t/(int)$k)) {
return $c.$v.'前';
}
}
}
echo format_date("1404600000");
?>

实例4:

<?php
function formatTime($date) {
$str = '';
$timer = strtotime($date);
$diff = $_SERVER['REQUEST_TIME'] - $timer;
$day = floor($diff / 86400);
$free = $diff % 86400;
if($day > 0) {
return $day."天前";
}else{
if($free>0){
$hour = floor($free / 3600);
$free = $free % 3600;
if($hour>0){
return $hour."小时前";
}else{
if($free>0){
$min = floor($free / 60);
$free = $free % 60;
if($min>0){
return $min."分钟前";
}else{
if($free>0){
return $free."秒前";
}else{
return '刚刚';
}
}
}else{
return '刚刚';
}
}
}else{
return '刚刚';
}
}
}
echo formatTime("2014-7-8 19:22:01");
?>

二、类的实现

<?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)/(60*60);
}
function getDay()
{
return ($this->createtime-$this->gettime)/(60*60*24);
}
function getMonth()
{
return ($this->createtime-$this->gettime)/(60*60*24*30);
}
function getYear()
{
return ($this->createtime-$this->gettime)/(60*60*24*30*12);
}
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把时间转换成几分钟前、几小时前、几天前的几个函数、类分享的更多相关文章

  1. js把字符串格式的时间转换成几秒前、几分钟前、几小时前、几天前等格式

    最近在做项目的时候,需要把后台返回的时间转换成几秒前.几分钟前.几小时前.几天前等的格式:后台返回的时间格式为:2015-07-30 09:36:10,需要根据当前的时间与返回的时间进行对比,最后显示 ...

  2. [jquery]将当前时间转换成yyyymmdd格式

    如题: function nowtime(){//将当前时间转换成yyyymmdd格式 var mydate = new Date(); var str = "" + mydate ...

  3. java时间类型的转换/获取当前时间/将时间转换成String/将String转换成时间

    对于我的脑子,我已经服气了...写了N遍的东西,就是记不住...既然记不住那就记下来... 利用java获取当前的时间(String类型,年-月-日 时:分:秒) //我要获取当前的日期 Date d ...

  4. 将本地时间转换成 UTC 时间,0时区时间

    // 将时间戳转换成日期格式: function timestampToTime(timestamp) { var date = new Date(timestamp);//时间戳为10位需*1000 ...

  5. JAVA CST时间 转换成Date

    Mybatis中处理Oracle时间类型是个比较麻烦的问题,特别是需要用到时间做比较的,可参考以下代码与思路: 格式化CST时间 SimpleDateFormat sdf = new SimpleDa ...

  6. vue ele 日期时间格式限制不能早于当天,时间转换成时间戳 进行比较

    <el-date-picker             value-format="yyyy-MM-dd HH:mm:ss"             v-model=&quo ...

  7. python中将HTTP头部中的GMT时间转换成datetime时间格式

    原文: https://blog.csdn.net/zoulonglong/article/details/80585716 需求背景:目前在做接口的自动化测试平台,由于接口用例执行后返回的结果中的时 ...

  8. php 中将完整的年月日时分秒的时间转换成 年月日的形式

    strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数), 将完整的时间格式转换成时间撮的形式,再去进 ...

  9. JS获取到时间转换成字符串类型

    JS获取到时间格式为:/Date(1479279579047)/ 提供一个函数可将其转换为“yyyy-MM-dd”和“yyyy-MM-dd hh:mm:ss” //时间日期转换成string func ...

随机推荐

  1. 在Ubuntu下利用Eclipse调试FFmpeg

    [编辑]第一步:准备编译环境 .tar.bz2 -2245/ ./configure -linux-i586.tar.gz 解压后得到jre1.7.0_17目录 最后通过java -version检查 ...

  2. 模拟多级复选框效果--jquery

    今天又次体会到jquery的强大了,做了个多级复选框的效果,代码总共就20+行就over了. 我又想用js来做一个看看,才写了几个方法就写不动了,兼容性要考虑很多,而且代码量直线上升. 主要分享下jq ...

  3. 信号量semaphore解析

    1 基础概念 信号量在创建时须要设置一个初始值,表示同一时候能够有几个任务能够訪问该信号量保护的共享资源.初始值为1就变成相互排斥锁(Mutex),即同一时候仅仅能有一个任务能够訪问信号量保护的共享资 ...

  4. python pymysql安装

    ==================pymysql=================== 由于 MySQLdb 模块还不支持 Python3.x,所以 Python3.x 如果想连接MySQL需要安装 ...

  5. 自己定义Application的未捕获异常处理

    近期由于工作原因.进行Android应用开发时发现应用在出现类似空指针等异常时,抛出未被捕获的异常.Android系统有默认的未捕获异常处理器,默认行为是结束对应的线程,但并不会直接退出程序,并且在应 ...

  6. centos7.0安装redis扩展

    1.下载 下载地址:https://github.com/phpredis/phpredis/ 文件名:phpredis-develop.zip 文件下载成功后,上传至/usr/local 2.安装 ...

  7. Ajax之基础总结

    一.Ajax 简介 Ajax 由 HTML.JavaScript技术.DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序.在详细探讨 Ajax 是 ...

  8. 九度OJ 1209:最小邮票数 (遍历)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2252 解决:741 题目描述: 有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值.     如,有1分,3分,3分,3分,4分五 ...

  9. Android代码绘制虚线、圆角、渐变效果图

    drawable文件夹放置动画/形状/选择器等属性文件,唯一的drawable文件名,不允许写错和拼错,否则运行报错.drawable文件夹底下的xml文件可以包括的标签共18个:animation- ...

  10. cocos2d-x3.6 生成带类图的离线文档

    我的博客:http://blog.csdn.net/dawn_moon cocos2d-x的官网有点慢,并且最新3.6的在线API文档居然没有了类图,不知道什么原因,之前2.2.6都是有的. 只是能够 ...