国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

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

2024-05-04 23:34:21
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php將日期格式轉換成xx天前的格式,涉及php時間操作及正則匹配的技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了php將日期格式轉換成xx天前格式的方法。分享給大家供大家參考。具體如下:

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

 

 
  1. // convert a date into a string that tells how long ago 
  2. // that date was.... eg: 2 days ago, 3 minutes ago. 
  3. function ago($d) { 
  4. $c = getdate(); 
  5. $p = array('year''mon''mday''hours''minutes''seconds'); 
  6. $display = array('year''month''day''hour''minute''second'); 
  7. $factor = array(0, 12, 30, 24, 60, 60); 
  8. $d = datetoarr($d); 
  9. for ($w = 0; $w < 6; $w++) { 
  10. if ($w > 0) { 
  11. $c[$p[$w]] += $c[$p[$w-1]] * $factor[$w]; 
  12. $d[$p[$w]] += $d[$p[$w-1]] * $factor[$w]; 
  13. if ($c[$p[$w]] - $d[$p[$w]] > 1) {  
  14. return ($c[$p[$w]] - $d[$p[$w]]).' '.$display[$w].'s ago'
  15. return ''
  16. // you can replace this if need be.  
  17. // This converts my dates returned from a mysql date string  
  18. // into an array object similar to that returned by getdate(). 
  19. function datetoarr($d) { 
  20. preg_match("/([0-9]{4})(//-)([0-9]{2})(//-)([0-9]{2})([0-9]{2})(//:)([0-9]{2})(//:)([0-9]{2})/",$d,$matches); 
  21. return array(  
  22. 'seconds' => $matches[10],  
  23. 'minutes' => $matches[8],  
  24. 'hours' => $matches[6],  
  25. 'mday' => $matches[5],  
  26. 'mon' => $matches[3],  
  27. 'year' => $matches[1],  
  28. ); 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜宾市| 利津县| 安仁县| 教育| 青岛市| 永年县| 石狮市| 宁河县| 合江县| 依安县| 尉犁县| 顺平县| 平和县| 汪清县| 金华市| 湘潭县| 内江市| 正阳县| 夹江县| 宁海县| 沙田区| 凤阳县| 平潭县| 深州市| 施甸县| 宝应县| 连平县| 达州市| 沿河| 新巴尔虎左旗| 黄平县| 上栗县| 沙河市| 洛阳市| 凤冈县| 永丰县| 永清县| 南安市| 泗水县| 革吉县| 东港市|