Home > Backend Development > PHP Tutorial > PHP function code example for outputting time difference

PHP function code example for outputting time difference

怪我咯
Release: 2023-03-13 10:04:01
Original
1338 people have browsed it

When learning PHP, it is often used to obtain the date of a certain period of time before or after now. Now that it has been collected, you can also expand and enrich it

PHP output time differencefunction

The code is as follows:

<?php  
date_default_timezone_set(&#39;PRC&#39;); //默认时区  
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>";  
?>
Copy after login

When learning PHP, it is often used to obtain the date of a certain period of time before or after now. Now that it has been collected, you can also expand and enrich

The code is as follows:

//获取当天的星期(1-7)
function GetWeek($times)
{
    $res = date(&#39;w&#39;, strtotime($times));
    if($res==0)
       $res=7;
    return $res;
}
//获取当天时间
function GetTime($times)
{
    $res = date(&#39;H:i&#39;, strtotime($times));
    return $res;
}
//获取现在过几月的的时间
function GetMonth($Month,$type=&#39;l&#39;)
{
    if(!strcmp($type,&#39;b&#39;))
      $res=date("Y-m-d H:i:s",strtotime("-$Month months"));
    if(!strcmp($type,&#39;l&#39;))
      $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=&#39;l&#39;)
{
  if(!strcmp($type,&#39;b&#39;))
     $res=date("Y-m-d H:i:s",strtotime("-$hours hour"));
  if(!strcmp($type,&#39;l&#39;))
     $res=date("Y-m-d H:i:s",strtotime("+$hours hour"));
  return $res;     
}
//间隔几分钟之前或之后的时间
function GetDiffMinute($Minute,$type=&#39;l&#39;)
{
  if(!strcmp($type,&#39;b&#39;))
     $res=date("Y-m-d H:i:s",strtotime("-$Minute minute"));
  if(!strcmp($type,&#39;l&#39;))
     $res=date("Y-m-d H:i:s",strtotime("+$Minute minute"));
  return $res;     
}
//间隔几秒之前或之后的时间
function GetDiffSec($sec,$type=&#39;l&#39;)
{
  if(!strcmp($type,&#39;b&#39;))
     $res=date("Y-m-d H:i:s",strtotime("-$sec second"));
  if(!strcmp($type,&#39;l&#39;))
     $res=date("Y-m-d H:i:s",strtotime("+$sec second"));
  return $res;     
}
//间隔几个星期之前或之后的时间
function GetDiffWeek($Week,$type=&#39;l&#39;)
{
  if(!strcmp($type,&#39;b&#39;))
     $res=date("Y-m-d H:i:s",strtotime("-$Week week"));
  if(!strcmp($type,&#39;l&#39;))
     $res=date("Y-m-d H:i:s",strtotime("+$Week week"));
  return $res;     
}
// 间隔几天之间的时间
function GetDiffDays($days,$type=&#39;l&#39;)
{
  if(!strcmp($type,&#39;b&#39;))
     $res=date("Y-m-d H:i:s",strtotime("-$days day"));
  if(!strcmp($type,&#39;l&#39;))
     $res=date("Y-m-d H:i:s",strtotime("+$days day"));
  return $res;     
}
//间隔几年之前或之后的时间
function GetDiffYears($year,$type=&#39;l&#39;)
{
  if(!strcmp($type,&#39;b&#39;))
     $res=date("Y-m-d H:i:s",strtotime("-$year year"));
  if(!strcmp($type,&#39;l&#39;))
     $res=date("Y-m-d H:i:s",strtotime("+$year year"));
  return $res;     
}
Copy after login

The above is the detailed content of PHP function code example for outputting time difference. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template