输出:
1 2 3 4 Jan-05-2002 Feb-01-2002 Jan-01-2001 Jan-01-1999strtotime 函数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 2 3 4 5 6 <?php $startdate = strtotime ( "2015-9-5" ); $enddate = strtotime ( "2015-9-18" ); //上面的php时间日期函数已经把日期变成了时间戳,就是变成了秒。这样只要让两数值相减,然后把秒变成天就可以了,比较的简单,如下: $days = round (( $enddate - $startdate )/3600/24) ; echo $days ; //days为得到的天数; ?>第二种
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 <? date_default_timezone_set( 'Asia/Shanghai' ); //以上一句为设置时区,其实不设也行,但是zde debug的时候会有提示,说什么不安全的函数…添上吧。 echo date ( 'Y-m-d H:i:s' ). ' 今天是' . date ( 'Y' ). '年的第' . date ( 'W' ). '周' ; $stime = '2005-11-03 10:08' ; echo "<br/><br/>***自出生(<font color=blue>$stime</font>)以来…:<br/><br/>" ; echo "今天是第<font color=red><b>" .Lnbsp(daysofnow( $stime ),3). "</b></font>天<br/>" ; echo "今天是第<font color=red><b>" .Lnbsp(weeksofnow( $stime ),3). "</b></font>周<br/>" ; echo "今天是第<font color=red><b>" .Lnbsp(monthsofnow( $stime ),3). "</b></font>个月<br/>" ; echo "今天是第<font color=red><b>" .Lnbsp(yearsofnow( $stime ),3). "</b></font>年<br/>" ; /* $output=sprintf(" 今天是第<font color=red><b>d</b></font>天<br/>今天是第< font color=red><b>d</b></font>周<br/>今天是第< font color=red><b>d</b></font>个月<br/>今天是第< font color=red><b>d</b></font>年<br/& gt;",daysofnow($stime),weeksofnow($stime),monthsofnow($stime),yearsofnow($stime)); echo $output; */ function weeksofnow( $stime ) { $ftime = strtotime ( $stime ); $fweeks = date ( 'w' , $ftime ); if ( $fweeks ==0) $fweeks =7; $nweeks = date ( 'w' ); if ( $nweeks ==0) $nweeks =7; $ftemp = strtotime ( date ( 'Y-m-d 00:00:00' , $ftime ))- $fweeks *60*60*24; $ntemp = strtotime ( date ( 'Y-m-d 00:00:00' ,time()))+(7- $nweeks )*60*60*24; //echo date('w',$ftemp)."<br/>....<br/>".date('w',$ntemp)."<br/>"; return ( $ntemp - $ftemp )/60/60/24/7; } function daysofnow( $stime ) { $ftime = strtotime ( $stime ); return ceil ( abs ((time()- $ftime )/(60*60*24))); } function monthsofnow( $stime ) { $ftime = strtotime ( $stime ); $fmonth = date ( 'm' , $ftime ); $fyear = date ( 'Y' , $ftime ); $nmonth = date ( 'm' ); $nyear = date ( 'Y' ); $result =( $nyear - $fyear )*12+ $nmonth - $fmonth +1; return $result ; } function yearsofnow( $stime ) { $ftime = strtotime ( $stime ); $fyear = date ( 'Y' , $ftime ); $nyear = date ( 'Y' ); return $nyear - $fyear +1; } // 下面的函数只是加空格用的,不是核心内容,只为美观 function Lnbsp( $data , $num ) { $result =trim( $data ); for ( $i = $num ; $i >= strlen ( $data ); $i --) { $result = ' ' . $result ; } return $result ; } ?>第三种 情况:明天,下个月和明年的日期,就可以用以下的代码:
1 2 3 4 5 6 7 $tomorrow = date ( 'Y-m-d' , mktime (0,0,0, date ( "m" ), date ( "d" )+1, date ( "Y" ))); $nextmonth = date ( 'Y-m' , mktime (0,0,0, date ( "m" )+1, date ( "d" )+1, date ( "Y" ))); $nextyear = date ( 'Y' , mktime (0,0,0, date ( "m" ), date ( "d" ), date ( "Y" )+1)); echo $tomorrow . '<br/>' ; echo $nextmonth . '<br/>' ; echo $nextyear . '<br/>' ;第四种情况:工作时间(刨除假日)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <? $startDate = "2001-12-12" ; $endDate = "2002-11-1" ; $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" ); //假期日期数组,比方国庆,五一,春节等 $endWeek =2; //周末是否双休.双休为2,仅仅星期天休息为1,没有休息为0 $beginUX = strtotime ( $startDate ); $endUX = strtotime ( $endDate ); for ( $n = $beginUX ; $n <= $endUX ; $n = $n +86400){ $week = date ( "w" , $n ); $MonDay = date ( "m-d" , $n ); if ( $endWeek ){ //去处周末休息 if ( $endWeek ==2){ if ( $week ==0|| $week ==6) continue ; } if ( $endWeek ==1){ if ( $week ==0) continue ; } } if (in_array( $MonDay , $holidayArr )) continue ; $totalHour +=10; //每天工作10小时 } echo "开始日期:$startDate<BR>" ; echo "结束日期:$endDate<BR>" ; echo "共花了" . $totalHour . "小时" ; ?>第五种情况:给出秒算小时
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <?php function transform( $sec ){ $output = '' ; $hours = floor ( $sec / 3600); $remainSeconds = $sec % 3600; $minutes = floor ( $remainSeconds / 60); $seconds = $sec - $hours * 3600 - $minutes * 60; if ( $sec >= 3600){ $output .= $hours . ' h / ' ; $output .= $minutes . ' m / ' ; } if ( $sec >= 60 && $sec < 3600){ $output .= $minutes . ' m / ' ; } return $output .= $seconds . ' s ' ; } echo transform(3231803); ?>