Home  >  Article  >  Backend Development  >  Detailed explanation of PHP date and time addition and subtraction program code_PHP tutorial

Detailed explanation of PHP date and time addition and subtraction program code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:00:01904browse

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
 代码如下 复制代码

/**
* PHP里的日期加减方法
* 琼台老屋
*/
// 第一步,假设有一个时间
$a = '2012-04-25 10:10:00';

// 第二步,获得这个日期的时间戳
$a_time = strtotime($a);

// 第三步,获得加五个月后的时间戳
$b_time = strtotime('+5 Month',$a_time);

// 第四部,把时间戳转换回日期格式
$b = date('Y-m-d H:i:s',$b_time);
echo '这是加了五个月后的日期'.$b;

// 如果你觉得以上代码过长也可以一行搞定
$b = date('Y-m-d H:i:s',strtotime('+'.$time.' Month',strtotime($a)));
echo '这是加了五个月后的日期'.$b;
?>

/**

* How to add and subtract dates in PHP
代码如下 复制代码

date_default_timezone_set('PRC'); //默认时区
echo "今天:",date("Y-m-d",time()),"
"; 
echo "今天:",date("Y-m-d",strtotime("18 june 2008")),"
"; 
echo "昨天:",date("Y-m-d",strtotime("-1 day")), "
"; 
echo "明天:",date("Y-m-d",strtotime("+1 day")), "
"; 
echo "一周后:",date("Y-m-d",strtotime("+1 week")), "
"; 
echo "一周零两天四小时两秒后:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "
"; 
echo "下个星期四:",date("Y-m-d",strtotime("next Thursday")), "
"; 
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."
"; 
echo "一个月前:".date("Y-m-d",strtotime("last month"))."
"; 
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."
"; 
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."
"; 
?>

* 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
The code is as follows Copy code
"; 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"))."
"; ?>

Output results

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

The code is as follows
 代码如下 复制代码

//获取当天的星期(1-7)
function GetWeek($times)
{
$res = date('w', strtotime($times));
if($res==0)
$res=7;
return $res;
}
//获取当天时间
function GetTime($times)
{
$res = date('H:i', strtotime($times));
return $res;
}
//获取现在过几月的的时间
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;
}
//获取当前时间
function GetCurrentDateTime()
{
$res=date("Y-m-d H:i:s",time());
return $res;
}
//获取当前时间隔几小时之前或之后的时间
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;
}
//间隔几分钟之前或之后的时间
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;
}
//间隔几秒之前或之后的时间
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;
}

//间隔几个星期之前或之后的时间
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;
}
// 间隔几天之间的时间
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;
}
//间隔几年之前或之后的时间
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;
}

Copy code
//Get the day of the week (1-7) function GetWeek($times)

{

$res = date('w', strtotime($times)); $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 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