php将日期格式转换成xx天前的格式
更新时间:2015年04月16日 10:11:43 作者:不吃皮蛋
这篇文章主要介绍了php将日期格式转换成xx天前的格式,涉及php时间操作及正则匹配的技巧,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了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程序设计有所帮助。
相关文章
Discuz! 5.0.0论坛程序中加入一段js代码,让会员点击下载附件前自动弹出提示窗口
Discuz! 5.0.0论坛程序中加入一段js代码,让会员点击下载附件前自动弹出提示窗口...2007-04-04PHP中CURL方法curl_setopt()函数的参数分享
PHP中CURL方法curl_setopt()函数的使用介绍,需要深入了解curl_setopt的朋友可以参考下2013-01-01php压缩HTML函数轻松实现压缩html/js/Css及注意事项
如何提高网页加载速度需要对网页怎样的优化等等,都是站长们所关心的问题,其实压缩网页的方法很多,本文将讲解一下php压缩HTML函数轻松实现压缩html/js/Css,感兴趣的朋友可以了解下,希望本文对你有所帮助2013-01-01PHP+.htaccess实现全站静态HTML文件GZIP压缩传输(一)
PHP+.htaccess实现全站静态HTML文件GZIP压缩传输(一)...2007-02-02
最新评论