PHP output time difference function code_PHP tutorial

WBOY
Release: 2016-07-21 15:13:13
Original
908 people have browsed it

PHP output time difference function

Copy code The code is as follows:

date_default_timezone_set('PRC'); //Default time zone
echo "Today:",date("Y-m-d",time()),"
";
echo "Today:",date("Y-m-d",strtotime("18 june 2008") ),"
";
echo "Yesterday:", date("Y-m-d",strtotime("-1 day")), "
";
echo "Tomorrow:", date("Y-m-d",strtotime("+1 day")), "
";
echo "One week later:",date("Y-m-d",strtotime("+1 week")), "
";
echo "One week, two days, four hours and two seconds later:",date("Y-m-d G:H:s",strtotime("+1 week 2 days, 4 hours 2 seconds")), "
";
echo "Next Thursday:", date("Y-m-d",strtotime("next Thursday")), "
";
echo "Last Monday:" .date("Y-m-d",strtotime("last Monday"))."
";
echo "One month ago:".date("Y-m-d",strtotime("last month"))."
";
echo "One month later:".date("Y-m-d",strtotime("+1 month"))."
";
echo "Ten years later:" .date("Y-m-d",strtotime("+10 year"))."
";
?>

When learning PHP, it is often used to get A date in a certain period of time before or after now. Now that it has been collected, you can also expand and enrich
Copy the code The code is as follows:

//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 the day
function GetTime($times)
{
    $res = date('H:i ', strtotime($times));
return $res;
}
//Get the time in several 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;
}
//Get the time before or after the current time interval in hours
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 time
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;
}
//The time before or after the interval in seconds
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;
}

//Time before or after a few weeks
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; ($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" ); 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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326523.htmlTechArticlePHP output time difference function copy code code is as follows: ?php date_default_timezone_set('PRC'); //Default time zone echo " Today:",date("Y-m-d",time()),"br"; echo "Today:",date("Y-m-d",strt...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!