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

Pamela ·
更新时间:2024-09-20
· 766 次阅读

本文实例讲述了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 < 6; $w++) { if ($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实现XML与数据格式进行转换类实例php将数组转换成csv格式文件输出的方法php导出csv格式数据并将数字转换成文本的思路以及代码分享php颜色转换函数hex-rgb(将十六进制格式转成十进制格式)PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)使用PHP+JavaScript将HTML页面转换为图片的实例分享分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为htmlPHP将HTML转换成文本的实现代码php中将html中的br换行符转换为文本输入中的换行符PHP转换文本框内容为HTML格式的方法



xx PHP

需要 登录 后方可回复, 如果你还没有账号请 注册新账号