Home Backend Development PHP Tutorial 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

Jul 13, 2016 pm 05:00 PM
php under code us date time Time difference program calculate Detailed explanation

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles