• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 输出时间差的函数代码实例

php 搞代码 3年前 (2022-01-22) 36次浏览 已收录 0个评论

在学习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<p style="color:transparent">。本文来源gao!%daima.com搞$代*!码网1</p><cite>搞代gaodaima码</cite>",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 输出时间差的函数代码实例的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 输出时间差的函数代码实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址