PHP日期时间加减程序代码详解
今天我们来看在php中计算两个时间之间的时间差.下面我们直接使用了data,strtotime与time三个函数就实现了.有需要的朋友可参考,今天要讲的这个例子,需求是这样的,得知某个日期时间,如:2012-04-25 10:10:00,我要在这个日期时间的基础上加上5个月并返回处理后的日期,结果:2012-04-25 10:10:00 加5个月等于 2012-09-25 10:10:00
结合PHP函数date()与strtotime()两个函数来实现大致也是这个意思,代码如下:
<?php /** * PHP里的日期加减方法 * 琼台老屋 */ // 第一步,假设有一个时间 $a = '2012-04-25 10:10:00'; // 第二步,获得这个日期的时间戳 $a_time = strtotime($a); // 第三步,获得加五个月后的时间戳 $b_time = strtotime('+5 Month', $a_time); // 第四部,把时间戳转换回日期格式 $b = date('Y-m-d H:i:s', $b_time); echo '这是加了五个月后的日期' . $b; // 如果你觉得以上代码过长也可以一行搞定 $b = date('Y-m-d H:i:s', strtotime('+' . $time . ' Month', strtotime($a))); echo '这是加了五个月后的日期' . $b; ?>
常用的计算时间,代码如下:
<?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>"; //输出结果 //今天:2013-06-07 //今天:2008-06-18 //昨天:2013-06-06 //明天:2013-06-08 //一周后:2013-06-14 //一周零两天四小时两秒后:2013-06-16 18:18:29 //下个星期四:2013-06-13 //上个周一:2013-06-03 //一个月前:2013-05-07 //一个月后:2013-07-07 //十年后:2023-06-07 ?>
这里再看一些日期加减函数,代码如下:
<?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; } ?>
本文地址:http://www.phprm.com/riqi/fs1428.html
转载随意,但请附上文章地址:-)