找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 742|回复: 0
打印 上一主题 下一主题

php将日期格式转换成xx天前的格式

[复制链接]

2588

主题

2588

帖子

7694

积分

论坛元老

Rank: 8Rank: 8

积分
7694
跳转到指定楼层
楼主
发表于 2018-2-14 05:46:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了php将日期格式转换成xx天前格式的方法。分享给大家供大家参考。具体如下:
这段代码可以把时间格式化成3天前,5秒前,2年前的形式
// convert a date into a string that tells how long ago
// that date was.... eg: 2 days ago, 3 minutes ago.
function ago($d) {
$c = getdate();
$p = array('year', 'mon', 'mday', 'hours', 'minutes', 'seconds');
$display = array('year', 'month', 'day', 'hour', 'minute', 'second');
$factor = array(0, 12, 30, 24, 60, 60);
$d = datetoarr($d);
for ($w = 0; $w  0) {
  $c[$p[$w]] += $c[$p[$w-1]] * $factor[$w];
  $d[$p[$w]] += $d[$p[$w-1]] * $factor[$w];
}
if ($c[$p[$w]] - $d[$p[$w]] > 1) {
  return ($c[$p[$w]] - $d[$p[$w]]).' '.$display[$w].'s ago';
}
}
return '';
}
// you can replace this if need be.
// This converts my dates returned from a mysql date string
// into an array object similar to that returned by getdate().
function datetoarr($d) {
preg_match("/([0-9]{4})(\\-)([0-9]{2})(\\-)([0-9]{2})([0-9]{2})(\\([0-9]{2})(\\([0-9]{2})/",$d,$matches);
return array(
'seconds' => $matches[10],
'minutes' => $matches[8],
'hours' => $matches[6],
'mday' => $matches[5],
'mon' => $matches[3],
'year' => $matches[1],
);
}
希望本文所述对大家的php程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)
  • php中将html中的br换行符转换为文本输入中的换行符
  • php颜色转换函数hex-rgb(将十六进制格式转成十进制格式)
  • php导出csv格式数据并将数字转换成文本的思路以及代码分享
  • PHP将HTML转换成文本的实现代码
  • php将数组转换成csv格式文件输出的方法
  • PHP实现XML与数据格式进行转换类实例
  • 分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html
  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享
  • PHP转换文本框内容为HTML格式的方法
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端