程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php將日期格式轉換成xx天前的格式

php將日期格式轉換成xx天前的格式

編輯:關於PHP編程

     這段代碼可以把時間格式化成3天前,5秒前,2年前的形式

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // 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], ); }
    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved