Home  >  Article  >  Backend Development  >  Several ways to get time in php

Several ways to get time in php

WBOY
WBOYOriginal
2016-07-25 09:05:291067browse
  1. function getWeekName($data,$format = '星期')
  2. {
  3. $week = date( "D ",$data);
  4. switch($week)
  5. {
  6. case "Mon ":
  7. $current = $format."一";
  8. break;
  9. case "Tue ":
  10. $current = $format."二";
  11. break;
  12. case "Wed ":
  13. $current = $format."三";
  14. break;
  15. case "Thu ":
  16. $current = $format."四";
  17. break;
  18. case "Fri ":
  19. $current = $format."五";
  20. break;
  21. case "Sat ":
  22. $current = $format."六"; break;
  23. case "Sun ":
  24. $current = $format."日";
  25. break;
  26. }
  27. return $current;
  28. }
  29. echo '今天是:'.getWeekName(time(),'星期');
  30. echo '
    ';
  31. echo '今天是:'.getWeekName(time(),'礼拜');
  32. echo '
    ';
  33. echo '2010-12-12是:'.getWeekName(strtotime('2010-12-12'),'礼拜');
  34. ?>
复制代码

4、获取类似文章发表的几小时前等效果的自定义函数

  1. function time2Units ($time)
  2. {
  3. $year = floor($time / 60 / 60 / 24 / 365);
  4. $time -= $year * 60 * 60 * 24 * 365;
  5. $month = floor($time / 60 / 60 / 24 / 30);
  6. $time -= $month * 60 * 60 * 24 * 30;
  7. $week = floor($time / 60 / 60 / 24 / 7);
  8. $time -= $week * 60 * 60 * 24 * 7;
  9. $day = floor($time / 60 / 60 / 24);
  10. $time -= $day * 60 * 60 * 24;
  11. $hour = floor($time / 60 / 60);
  12. $time -= $hour * 60 * 60;
  13. $minute = floor($time / 60);
  14. $time -= $minute * 60;
  15. $second = $time;
  16. $elapse = '';
  17. $unitArr = array('年' =>'year', '个月'=>'month', '周'=>'week', '天'=>'day',
  18. '小时'=>'hour', '分钟'=>'minute', '秒'=>'second'
  19. );
  20. foreach ( $unitArr as $cn => $u )
  21. {
  22. if ( $$u > 0 )
  23. {
  24. $elapse = $$u . $cn;
  25. break;
  26. }
  27. }
  28. return $elapse;
  29. }
  30. $past = 2052345678; // 发布日期
  31. $now = time(); // 当前日期
  32. $diff = $now - $past;//相差值
  33. echo '发表于' . time2Units($diff) . '前';
  34. ?>
复制代码

另一种,个人认为比较好的:

  1. function time_tran($the_time){
  2. $now_time = date("Y-m-d H:i:s",time()+8*60*60);
  3. $now_time = strtotime($now_time);
  4. $show_time = strtotime($the_time);
  5. $dur = $now_time - $show_time;
  6. if($dur < 0){
  7. return $the_time;
  8. }else{
  9. if($dur < 60){
  10. return $dur.'秒前';
  11. }else{
  12. if($dur < 3600){
  13. return floor($dur/60).'分钟前';
  14. }else{
  15. if($dur < 86400){
  16. return floor($dur/3600).'小时前';
  17. }else{
  18. if($dur < 259200){//3天内
  19. return floor($dur/86400).'天前';
  20. }else{
  21. return $the_time;
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }
  28. ?>
复制代码

5. Calculate the time difference based on the difference between the two times.

  1. function getTime( $val ){
  2. if($val>0){
  3. $nTime['nDay'] = (int)($val/(3600*24));
  4. $nTime['nHour'] = (int)($val%(3600*24)/3600);
  5. $nTime['nMin'] = (int)($val%(3600*24)%3600/60 );
  6. $nTime['nSec'] = (int)($val%(3600*24)%3600%60);
  7. }
  8. return $nTime;
  9. }
  10. function getStrTime( $val ){
  11. $aTime = getTime($val);
  12. $dtoc = array('nDay'=>'day','nHour'=>'hour','nMin'=>'minute','nSec'=>'second ');
  13. if( $aTime ){
  14. foreach( $aTime as $k=>$v){
  15. if($v){
  16. $cTime .= $v.$dtoc[$k];
  17. }
  18. }
  19. }else{
  20. $cTime = 'Ended';
  21. }
  22. return $cTime;
  23. }
  24. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn