PHP输出时间差函数
<?php <BR>date_default_timezone_set('PRC'); //默认时区 <BR>echo "今天:",date("Y-m-d",time()),"<br>"; <BR>echo "今天:",date("Y-m-d",strtotime("18 june 2008")),"<br>"; <BR>echo "昨天:",date("Y-m-d",strtotime("-1 day")), "<br>"; <BR>echo "明天:",date("Y-m-d",strtotime("+1 day")), "<br>"; <BR>echo "一周后:",date("Y-m-d",strtotime("+1 week")), "<br>"; <BR>echo "一周零两天四小时两秒后:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "<br>"; <BR>echo "下个星期四:",date("Y-m-d",strtotime("next Thursday")), "<br>"; <BR>echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>"; <BR>echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>"; <BR>echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>"; <BR>echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>"; <BR>?> <BR>
在学习PHP 的时候,经常会用到获取现在之前或之后,某个时间段的日期。现在已经进行收集,大家同时也可以进行扩展丰富
//获取当天的星期(1-7)<BR>function GetWeek($times)<BR>{<BR> $res = date('w', strtotime($times));<BR> if($res==0)<BR> $res=7;<BR> return $res;<BR>}<BR>//获取当天时间<BR>function GetTime($times)<BR>{<BR> $res = date('H:i', strtotime($times));<BR> return $res;<BR>}<BR>//获取现在过几月的的时间<BR>function GetMonth($Month,$type='l')<BR>{<BR> if(!strcmp($type,'b'))<BR> $res=date("Y-m-d H:i:s",strtotime("-$Month months"));<BR> if(!strcmp($type,'l'))<BR> $res=date("Y-m-d H:i:s",strtotime("+$Month months"));<BR> return $res;<BR>}<BR>//获取当前时间<BR>function GetCurrentDateTime()<BR>{<BR> $res=date("Y-m-d H:i:s",time());<BR> return $res;<BR>}<BR>//获取当前时间隔几小时之前或之后的时间<BR>function GetDiffHours($hours,$type='l')<BR>{<BR> if(!strcmp($type,'b'))<BR> $res=date("Y-m-d H:i:s",strtotime("-$hours hour"));<BR> if(!strcmp($type,'l'))<BR> $res=date("Y-m-d H:i:s",strtotime("+$hours hour"));<BR> return $res; <BR>}<BR>//间隔几分钟之前或之后的时间<BR>function GetDiffMinute($Minute,$type='l')<BR>{<BR> if(!strcmp($type,'b'))<BR> $res=date("Y-m-d H:i:s",strtotime("-$Minute minute"));<BR> if(!strcmp($type,'l'))<b>/本文来源gao@!dai!ma.com搞$$代^@码5网@</b><strong>搞代gaodaima码</strong><BR> $res=date("Y-m-d H:i:s",strtotime("+$Minute minute"));<BR> return $res; <BR>}<BR>//间隔几秒之前或之后的时间<BR>function GetDiffSec($sec,$type='l')<BR>{<BR> if(!strcmp($type,'b'))<BR> $res=date("Y-m-d H:i:s",strtotime("-$sec second"));<BR> if(!strcmp($type,'l'))<BR> $res=date("Y-m-d H:i:s",strtotime("+$sec second"));<BR> return $res; <BR>}</P><P>//间隔几个星期之前或之后的时间<BR>function GetDiffWeek($Week,$type='l')<BR>{<BR> if(!strcmp($type,'b'))<BR> $res=date("Y-m-d H:i:s",strtotime("-$Week week"));<BR> if(!strcmp($type,'l'))<BR> $res=date("Y-m-d H:i:s",strtotime("+$Week week"));<BR> return $res; <BR>}<BR>// 间隔几天之间的时间<BR>function GetDiffDays($days,$type='l')<BR>{<BR> if(!strcmp($type,'b'))<BR> $res=date("Y-m-d H:i:s",strtotime("-$days day"));<BR> if(!strcmp($type,'l'))<BR> $res=date("Y-m-d H:i:s",strtotime("+$days day"));<BR> return $res; <BR>}<BR>//间隔几年之前或之后的时间<BR>function GetDiffYears($year,$type='l')<BR>{<BR> if(!strcmp($type,'b'))<BR> $res=date("Y-m-d H:i:s",strtotime("-$year year"));<BR> if(!strcmp($type,'l'))<BR> $res=date("Y-m-d H:i:s",strtotime("+$year year"));<BR> return $res; <BR>}<BR>