PHP输出时间差函数代码
2013-01-28 00:00
741 查看
PHP输出时间差函数
在学习PHP 的时候,经常会用到获取现在之前或之后,某个时间段的日期。现在已经进行收集,大家同时也可以进行扩展丰富
<?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time()),"<br>"; echo "今天:",date("Y-m-d",strtotime("18 june 2008")),"<br>"; echo "昨天:",date("Y-m-d",strtotime("-1 day")), "<br>"; echo "明天:",date("Y-m-d",strtotime("+1 day")), "<br>"; echo "一周后:",date("Y-m-d",strtotime("+1 week")), "<br>"; echo "一周零两天四小时两秒后:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "<br>"; echo "下个星期四:",date("Y-m-d",strtotime("next Thursday")), "<br>"; echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>"; echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>"; echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>"; echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>"; ?>
在学习PHP 的时候,经常会用到获取现在之前或之后,某个时间段的日期。现在已经进行收集,大家同时也可以进行扩展丰富
//获取当天的星期(1-7) function GetWeek($times) { $res = date('w', strtotime($times)); if($res==0) $res=7; return $res; } //获取当天时间 function GetTime($times) { $res = date('H:i', strtotime($times)); return $res; } //获取现在过几月的的时间 function GetMonth($Month,$type='l') { if(!strcmp($type,'b')) $res=date("Y-m-d H:i:s",strtotime("-$Month months")); if(!strcmp($type,'l')) $res=date("Y-m-d H:i:s",strtotime("+$Month months")); return $res; } //获取当前时间 function GetCurrentDateTime() { $res=date("Y-m-d H:i:s",time()); return $res; } //获取当前时间隔几小时之前或之后的时间 function GetDiffHours($hours,$type='l') { if(!strcmp($type,'b')) $res=date("Y-m-d H:i:s",strtotime("-$hours hour")); if(!strcmp($type,'l')) $res=date("Y-m-d H:i:s",strtotime("+$hours hour")); return $res; } //间隔几分钟之前或之后的时间 function GetDiffMinute($Minute,$type='l') { if(!strcmp($type,'b')) $res=date("Y-m-d H:i:s",strtotime("-$Minute minute")); if(!strcmp($type,'l')) $res=date("Y-m-d H:i:s",strtotime("+$Minute minute")); return $res; } //间隔几秒之前或之后的时间 function GetDiffSec($sec,$type='l') { if(!strcmp($type,'b')) $res=date("Y-m-d H:i:s",strtotime("-$sec second")); if(!strcmp($type,'l')) $res=date("Y-m-d H:i:s",strtotime("+$sec second")); return $res; } //间隔几个星期之前或之后的时间 function GetDiffWeek($Week,$type='l') { if(!strcmp($type,'b')) $res=date("Y-m-d H:i:s",strtotime("-$Week week")); if(!strcmp($type,'l')) $res=date("Y-m-d H:i:s",strtotime("+$Week week")); return $res; } // 间隔几天之间的时间 function GetDiffDays($days,$type='l') { if(!strcmp($type,'b')) $res=date("Y-m-d H:i:s",strtotime("-$days day")); if(!strcmp($type,'l')) $res=date("Y-m-d H:i:s",strtotime("+$days day")); return $res; } //间隔几年之前或之后的时间 function GetDiffYears($year,$type='l') { if(!strcmp($type,'b')) $res=date("Y-m-d H:i:s",strtotime("-$year year")); if(!strcmp($type,'l')) $res=date("Y-m-d H:i:s",strtotime("+$year year")); return $res; }
相关文章推荐
- PHP输出时间差函数代码
- PHP输出时间差函数代码
- PHP格式化显示时间date()函数代码
- PHP date函数输出时间少8小时的解决方法
- php中microtime函数记录代码执行时间
- php输出时间与当前时间不一致(时间函数出错解决办法)
- 获取内容发布时间与当前的时差 Monxin专用(PHP代码函数)
- unixtime时间戳 转 年月日(PHP代码函数)
- 根据指定两个日期计算出这些时间内有多少天是周末 php程序函数代码
- 根据指定两个日期计算出这些时间内有多少天是周末 php程序函数代码
- php实现的DateDiff和DateAdd时间函数代码分享
- 利用MySQL的函数实现PHP中时间的格式化输出
- Apache+php,我想在某个特定的时间(比如每个月底)执行某段代码(某个函数),可以办得到
- php中用microtime()函数来测试代码运行时间
- 年月日转 unixtime时间戳(PHP代码函数)
- php date 函数输出的时间相差20小时
- PHP uniqid()函数可用于生成不重复的唯一标识符,该函数基于微秒级当前时间戳。在高并发或者间隔时长极短(如循环代码)的情况下,会出现大量重复数据。即使使用了第二个参数,也会重复,最好的方案是结
- php实现的DateDiff和DateAdd时间函数代码分享
- PHP显示日期时间的代码