当前位置:首页 > PHP教程 > php日期 > 列表

非常全面的php日期时间运算汇总

发布:smiling 来源: PHP粉丝网  添加日期:2021-06-25 11:15:04 浏览: 评论:0 

这篇文章主要整理了关于php日期时间运算相关内容,涉及知识点较为全面,感兴趣的小伙伴们可以参考一下。

实例讲解之前,先来介绍几个核心函数:

mktime 函数

mktime() 函数返回一个日期的 Unix 时间戳。

参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。

参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。

语法:mktime(hour,minute,second,month,day,year,is_dst)

参数               描述

hour       可选。规定小时。

minute   可选。规定分钟。

second   可选。规定秒。

month    可选。规定用数字表示的月。

day         可选。规定天。

year        可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。

is_dst  可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。 

自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。 

例子:mktime() 函数对于日期运算和验证非常有用。它可以自动校正越界的输入:  

  1. <?php  
  2. echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));  
  3. echo(date("M-d-Y",mktime(0,0,0,14,1,2001)));  
  4. echo(date("M-d-Y",mktime(0,0,0,1,1,2001)));  
  5. echo(date("M-d-Y",mktime(0,0,0,1,1,99)));  
  6. ?> 

输出:

  1. Jan-05-2002  
  2. Feb-01-2002  
  3. Jan-01-2001  
  4. Jan-01-1999  

strtotime 函数

strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。

语法:strtotime(time,now)

参数      描述

time    规定要解析的时间字符串。

now     用来计算返回值的时间戳。如果省略该参数,则使用当前时间。   

一周之后:  strtotime("+1 week") ;

一周之前:  strtotime("-1 week") ;

一月之后:  strtotime("+1 months") ;

一天之后:  strtotime("+1 days") ;     

30秒之后 strtotime( " +30 seconds " );

20分钟之后 strtotime( " +20 minutes " );

12个小时之后 strtotime( " +12 hours " );

date 函数

date() 函数格式化一个本地时间/日期。

语法

date(format,timestamp)

date_default_timezone_set 函数

date_default_timezone_set() 函数设置用在脚本中所有日期/时间函数的默认时区。

date_default_timezone_set(timezone)

实例

第一种情况是没有数据库,只是得到的日期值进行比较的话,那就得完全用php的时间日期函数计算了,如下:

比如要计算2015-9-5到2015-9-18还有多少天: 

  1. <?php  
  2. $startdate=strtotime("2015-9-5");  
  3. $enddate=strtotime("2015-9-18"); //上面的php时间日期函数已经把日期变成了时间戳,就是变成了秒。这样只要让两数值相减,然后把秒变成天就可以了,比较的简单,如下:  
  4. $days=round(($enddate-$startdate)/3600/24) ;  
  5. echo $days//days为得到的天数;  
  6. ?> 

第二种 孩子的成长

  1. <?  
  2. date_default_timezone_set('Asia/Shanghai');  
  3. //以上一句为设置时区,其实不设也行,但是zde debug的时候会有提示,说什么不安全的函数…添上吧。  
  4.    
  5. echo date('Y-m-d H:i:s').' 今天是'.date('Y').'年的第'.date('W').'周';  
  6.    
  7. $stime='2005-11-03 10:08';  
  8. echo "<br/><br/>***自出生(<font color=blue>$stime</font>)以来…:<br/><br/>";  
  9. echo "今天是第<font color=red><b>".Lnbsp(daysofnow($stime),3)."</b></font>天<br/>";  
  10. echo "今天是第<font color=red><b>".Lnbsp(weeksofnow($stime),3)."</b></font>周<br/>";  
  11. echo "今天是第<font color=red><b>".Lnbsp(monthsofnow($stime),3)."</b></font>个月<br/>";  
  12. echo "今天是第<font color=red><b>".Lnbsp(yearsofnow($stime),3)."</b></font>年<br/>";  
  13. /*  
  14. $output=sprintf(" 今天是第<font color=red><b>%03d</b></font>天<br/>今天是第< font color=red><b>%03d</b></font>周<br/>今天是第< font color=red><b>%03d</b></font>个月<br/>今天是第< font color=red><b>%03d</b></font>年<br/& gt;",daysofnow($stime),weeksofnow($stime),monthsofnow($stime),yearsofnow($stime));  
  15. echo $output;  
  16. */ 
  17.    
  18. function weeksofnow($stime)  
  19. {  
  20.  $ftime=strtotime($stime);  
  21.  $fweeks=date('w',$ftime);  
  22.  if ($fweeks==0) $fweeks=7;  
  23.  $nweeks=date('w');  
  24.  if ($nweeks==0) $nweeks=7;  
  25.  $ftemp=strtotime(date('Y-m-d 00:00:00',$ftime))-$fweeks*60*60*24;  
  26.  $ntemp=strtotime(date('Y-m-d 00:00:00',time()))+(7-$nweeks)*60*60*24;  
  27.  //echo date('w',$ftemp)."<br/>....<br/>".date('w',$ntemp)."<br/>";  
  28.  return ($ntemp-$ftemp)/60/60/24/7;  
  29. }  
  30.    
  31. function daysofnow($stime)  
  32. {  
  33.  $ftime=strtotime($stime);  
  34.  return ceil(abs((time()-$ftime)/(60*60*24)));  
  35. }  
  36.    
  37. function monthsofnow($stime)  
  38. {  
  39.  $ftime=strtotime($stime);  
  40.  $fmonth=date('m',$ftime);  
  41.  $fyear=date('Y',$ftime);  
  42.  $nmonth=date('m');  
  43.  $nyear=date('Y');  
  44.  $result=($nyear-$fyear)*12+$nmonth-$fmonth+1;  
  45.  return $result;  
  46. }  
  47.    
  48. function yearsofnow($stime)  
  49. {  
  50.  $ftime=strtotime($stime);  
  51.  $fyear=date('Y',$ftime);  
  52.  $nyear=date('Y');  
  53.  return $nyear-$fyear+1;  
  54. }  
  55.    
  56. // 下面的函数只是加空格用的,不是核心内容,只为美观  
  57. function Lnbsp($data,$num)  
  58. {  
  59.  $result=trim($data);  
  60.  for($i=$num;$i>=strlen($data);$i--) {  
  61.  $result='&nbsp;'.$result;  
  62.  }  
  63.  return $result;  
  64. }  
  65. ?> 

第三种 情况:明天,下个月和明年的日期,就可以用以下的代码:

  1. $tomorrow = date('Y-m-d',mktime (0,0,0,date("m"),date("d")+1,date("Y")));  
  2. $nextmonth = date('Y-m',mktime (0,0,0,date("m")+1,date("d")+1,date("Y")));  
  3. $nextyear = date('Y',mktime (0,0,0,date("m"),date("d"),date("Y")+1));  
  4.    
  5. echo $tomorrow.'<br/>';  
  6. echo $nextmonth.'<br/>';  
  7. echo $nextyear.'<br/>';  

第四种情况:工作时间(刨除假日) 

  1. <?  
  2. $startDate="2001-12-12";  
  3. $endDate="2002-11-1";  
  4.    
  5. $holidayArr=array("05-01","05-02","10-01","10-01","10-02","10-03","10-04","10-05","01-26","01-27","01-28","01-29");  
  6.  //假期日期数组,比方国庆,五一,春节等  
  7. $endWeek=2;  
  8.  //周末是否双休.双休为2,仅仅星期天休息为1,没有休息为0  
  9.    
  10. $beginUX=strtotime($startDate);  
  11. $endUX=strtotime($endDate);  
  12.    
  13. for($n=$beginUX;$n<=$endUX;$n=$n+86400){  
  14.  $week=date("w",$n);  
  15.  $MonDay=date("m-d",$n);  
  16.  if($endWeek){//去处周末休息  
  17.  if($endWeek==2){  
  18.  if($week==0||$week==6) continue;  
  19.  }  
  20.  if($endWeek==1){  
  21.  if($week==0) continue;  
  22.  }  
  23.  }  
  24.  if(in_array($MonDay,$holidayArr)) continue;  
  25.  $totalHour+=10;//每天工作10小时  
  26. }  
  27. echo "开始日期:$startDate<BR>";  
  28. echo "结束日期:$endDate<BR>";  
  29. echo "共花了".$totalHour."小时";  
  30. ?> 

第五种情况:给出秒算小时

  1. <?php  
  2. function transform($sec){  
  3.    
  4.  $output = '';  
  5.    
  6.  $hours = floor($sec / 3600);  
  7.  $remainSeconds = $sec % 3600;  
  8.    
  9.  $minutes = floor($remainSeconds / 60);  
  10.  $seconds = $sec - $hours * 3600 - $minutes * 60;  
  11.    
  12.  if($sec >= 3600){  
  13.  $output .= $hours.' h / ';  
  14.  $output .= $minutes.' m / ';  
  15.  }  
  16.    
  17.  if($sec >= 60 && $sec < 3600){  
  18.  $output .= $minutes.' m / ';  
  19.  }  
  20.    
  21.  return $output .= $seconds.' s ';  
  22. }  
  23.    
  24. echo transform(3231803);  
  25.    
  26. ?>

Tags: php日期时间

分享到: