Detailed explanation of PHP date and time addition and subtraction program code_PHP tutorial
WBOYOriginal
2016-07-13 17:00:01968browse
Today we will look at calculating the time difference between two times in PHP. Below we directly use the three functions of data, strtotime and time to implement it. Friends in need can refer to it.
The requirements for the example we are going to talk about today are as follows. Get a certain date and time,
For example: 2012-04-25 10:10:00
I want to add 5 months to this date and time and return the processed date
Result: 2012-04-25 10:10:00 plus 5 months equals 2012-09-25 10:10:00
Combining the PHP functions date() and strtotime() can achieve roughly the same meaning,
* Qiongtai Old House
*/
// The first step, assuming there is a time
$a = '2012-04-25 10:10:00';
// The second step is to get the timestamp of this date
$a_time = strtotime($a);
// The third step is to get the timestamp after adding five months
$b_time = strtotime('+5 Month',$a_time);
// Part 4, convert the timestamp back to date format
$b = date('Y-m-d H:i:s',$b_time);
echo 'This is the date after adding five months'.$b;
// If you think the above code is too long, you can do it in one line
$b = date('Y-m-d H:i:s',strtotime('+'.$time.' Month',strtotime($a)));
echo 'This is the date after adding five months'.$b;
?>
Commonly used calculation times
Today: 2013-06-07
Today: 2008-06-18
Yesterday: 2013-06-06
Tomorrow: 2013-06-08
One week later: 2013-06-14
One week, two days, four hours and two seconds later: 2013-06-16 18:18:29
Next Thursday: 2013-06-13
Last Monday: 2013-06-03
One month ago: 2013-05-07
One month later: 2013-07-07
Ten years later: 2023-06-07
Let’s look at some date addition and subtraction functions
//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')
$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
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;
}
//The time before or after the interval in minutes
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;
}
//How many weeks before or after the interval
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;
}
// The 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;
}
//The time before or after the interval of several years
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.bkjia.com/PHPjc/631270.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631270.htmlTechArticleToday we look at calculating the time difference between two times in php. Below we directly use data, strtotime The three functions with time are implemented. Friends in need can refer to it. Today I’m going to talk about...
Statement:
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