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,
The code is as follows | Copy code | ||||||||||||||||||||
Output results Today: 2013-06-07 Let’s look at some date addition and subtraction functions
{ { 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
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...
Related labels:
source:php.cn
Previous article:The difference between strlen, mb_strlen, substr(), mb_substr() and mb_strcut in php_PHP tutorial
Next article:Determine whether an empty file directory has read and write permissions in PHP_PHP Tutorial
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
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|