php將日期格式轉(zhuǎn)換成xx天前的格式
更新時間:2015年04月16日 10:11:43 作者:不吃皮蛋
這篇文章主要介紹了php將日期格式轉(zhuǎn)換成xx天前的格式,涉及php時間操作及正則匹配的技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了php將日期格式轉(zhuǎn)換成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程序設(shè)計有所幫助。
您可能感興趣的文章:
- PHP實現(xiàn)XML與數(shù)據(jù)格式進行轉(zhuǎn)換類實例
- php將數(shù)組轉(zhuǎn)換成csv格式文件輸出的方法
- php導出csv格式數(shù)據(jù)并將數(shù)字轉(zhuǎn)換成文本的思路以及代碼分享
- php顏色轉(zhuǎn)換函數(shù)hex-rgb(將十六進制格式轉(zhuǎn)成十進制格式)
- PHP+Mysql日期時間如何轉(zhuǎn)換(UNIX時間戳和格式化日期)
- 使用PHP+JavaScript將HTML頁面轉(zhuǎn)換為圖片的實例分享
- 分享php代碼將360瀏覽器導出的favdb的sqlite數(shù)據(jù)庫文件轉(zhuǎn)換為html
- PHP將HTML轉(zhuǎn)換成文本的實現(xiàn)代碼
- php中將html中的br換行符轉(zhuǎn)換為文本輸入中的換行符
- PHP轉(zhuǎn)換文本框內(nèi)容為HTML格式的方法
相關(guān)文章
學習php設(shè)計模式 php實現(xiàn)命令模式(command)
這篇文章主要介紹了php設(shè)計模式中的命令模式,使用php實現(xiàn)命令模式,感興趣的小伙伴們可以參考一下2015-12-12
使用php數(shù)據(jù)緩存技術(shù)提高執(zhí)行效率
使用php緩存技術(shù)時為了提高效率。在大量的并發(fā)訪問面前,獲取數(shù)據(jù)可能成為效率的瓶頸,PHP實際開發(fā)之中針對數(shù)據(jù)處理進行緩存。2022-12-12
Cannot modify header information錯誤解決方法
Warning: Cannot modify header information - headers already sent by出錯的原因2008-10-10
PHP調(diào)用FFMpeg實現(xiàn)音視頻操作的示例詳解
這篇文章主要為大家詳細介紹了PHP如何調(diào)用FFMpeg實現(xiàn)簡單的音視頻操作,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解下2023-10-10
php htmlentities和htmlspecialchars 的區(qū)別
很多人都以為htmlentities跟htmlspecialchars的功能是一樣的,都是格式化html代碼的,我以前也曾這么認為,但是今天我發(fā)現(xiàn)并不是這樣的。2008-08-08
windows服務(wù)器iis+php獲得錯誤信息的配置方法
最近技術(shù)在服務(wù)器上執(zhí)行代碼時總是顯示空白信息,因為本地測試正常的,但服務(wù)器上就有問題了,默認都是不顯示php代碼的錯誤信息的,可以通過如下設(shè)置就可以了2025-02-02

