PHP outputs the time difference function code

  • 2020-05-27 04:37:53
  • OfStack

PHP outputs the time difference function


<?php  
date_default_timezone_set('PRC'); // The default time zone   
echo " Today, :",date("Y-m-d",time()),"<br>";  
echo " Today, :",date("Y-m-d",strtotime("18 june 2008")),"<br>";  
echo " Yesterday, :",date("Y-m-d",strtotime("-1 day")), "<br>";  
echo " Tomorrow, :",date("Y-m-d",strtotime("+1 day")), "<br>";  
echo "1 Weeks later, :",date("Y-m-d",strtotime("+1 week")), "<br>";  
echo "1 Weeks and two days 4 In two hours :",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "<br>";  
echo " Next week, 4:",date("Y-m-d",strtotime("next Thursday")), "<br>";  
echo " Last week, 1:".date("Y-m-d",strtotime("last Monday"))."<br>";  
echo "1 Months ago, :".date("Y-m-d",strtotime("last month"))."<br>";  
echo "1 After one month :".date("Y-m-d",strtotime("+1 month"))."<br>";  
echo "10 Years later, :".date("Y-m-d",strtotime("+10 year"))."<br>";  
?> 

When I was studying PHP, I often used to get the date of a period of time before or after the present. Now that you've collected it, you can also expand it

// Get the day of the week ( 1-7 ) 
function GetWeek($times)
{
    $res = date('w', strtotime($times));
    if($res==0)
       $res=7;
    return $res;
}
// Get the time of day 
function GetTime($times)
{
    $res = date('H:i', strtotime($times));
    return $res;
}
// Get a few months now 
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;
}
// Get the current time 
function GetCurrentDateTime()
{
    $res=date("Y-m-d H:i:s",time());
    return $res;
}
// Gets the time before or after the current time several hours apart 
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;     
}
// A few minutes before or after 
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;     
}
// A few seconds before or after 
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;     
}
// A few weeks before or after 
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;     
}
//  A period of time between days 
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;     
}
// Years before or after 
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;     
}


Related articles: