首页 > 后端开发 > php教程 > php获取时间的几种方法

php获取时间的几种方法

WBOY
发布: 2016-07-25 09:05:29
原创
1149 人浏览过
  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 return $the_time;
  7. }else{
  8. if($dur return $dur.'秒前';
  9. }else{
  10. if($dur return floor($dur/60).'分钟前';
  11. }else{
  12. if($dur return floor($dur/3600).'小时前';
  13. }else{
  14. if($dur return floor($dur/86400).'天前';
  15. }else{
  16. return $the_time;
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }
  23. ?>
复制代码

5、根据两时间差具体算相差时间

  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'=>'天','nHour'=>'小时','nMin'=>'分','nSec'=>'秒');
  13. if( $aTime ){
  14. foreach( $aTime as $k=>$v){
  15. if($v){
  16. $cTime .= $v.$dtoc[$k];
  17. }
  18. }
  19. }else{
  20. $cTime = '已结止';
  21. }
  22. return $cTime;
  23. }
  24. ?>
复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板