Example of date addition and subtraction methods in PHP, php date example_PHP tutorial

WBOY
Release: 2016-07-13 10:20:25
Original
1097 people have browsed it

Examples of date addition and subtraction methods in PHP, php date examples

Almost all programmers engaged in program development encounter time processing problems, and PHP development is the same. Fortunately, PHP provides a lot of date and time functions. As long as you use these functions frequently and in combination, practice makes perfect in date and time processing.

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

This requirement seems simple, but it is still a bit tricky because PHP does not directly provide the addition and subtraction of date and time in the format of yyyy-mm-dd hh:ii:ss, so it can only be achieved through timestamps. Timestamps are the standard format for program conversions and are accurate to seconds. PHP can convert a variety of date formats into timestamps, and can convert timestamps back into various date formats. Combining these two features, we roughly implement three steps. First, convert the original time into a timestamp, and then add and subtract it. , and finally convert back to date format.

Of course, this is the implementation principle. Combining the PHP functions date() and strtotime() can achieve roughly the same meaning. Please see the example code

Copy code The code is as follows:

/**
* How to add and subtract dates in PHP
* 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;
?>

Regarding the use of date() function and strtotime() function, we will not introduce them in detail here. If you need children's shoes, you can read my previous articles introducing related functions or go to php.net to read the manual.

php date addition and subtraction problem

Don’t think PHP is so stupid, use time() for the current time, and find the most direct method according to your needs.
$time=time();
for($i=1;$i<=5;$i++){
$s=getdate($time-$i*24*3600);
$day[$i][mdy]=$s[mday];
$day[$i][mon]=$s[mon];
}
This way you get the past 5 If the month and day of the day are determined, replace - with + to get the month and day of the next 5 days.
However, I find that you don’t quite understand the definition of timestamp.
The next five days are time()+5*24*3600,
The past five days are time()-5*24*3600,
Why can’t we use timestamp?

How to implement PHP date addition and subtraction countdown code in HTML

It is recommended to use javascript language to write code. It is more troublesome to use PHP to complete this.

You can search online for specific countdown codes. There are many similar codes.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/867245.htmlTechArticleExample of date addition and subtraction methods in PHP, php date example Almost all programmers engaged in program development encounter time Dealing with problems, PHP development is the same. Fortunately, PHP provides a lot of information about...
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