国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新闻| 嘉峪关市| 兖州市| 皮山县| 朝阳县| 公安县| 桐梓县| 承德县| 济源市| 曲周县| 柳河县| 凤翔县| 讷河市| 抚州市| 原阳县| 宜阳县| 扎兰屯市| 彩票| 桐乡市| 云龙县| 云安县| 武平县| 余庆县| 青海省| 石嘴山市| 图们市| 仁寿县| 法库县| 宜州市| 衡山县| 武胜县| 迭部县| 桐梓县| 漳平市| 阿克苏市| 元氏县| 南靖县| 天柱县| 六盘水市| 东乡族自治县| 黄山市|