Example of the date addition and subtraction method in PHP and the php date example

Source: Internet
Author: User

Example of the date addition and subtraction method in PHP and the php date example

Almost all programmers engaged in program development encounter time processing problems, and PHP development is also the same. Fortunately, PHP provides a lot of date and time functions. As long as these functions are frequently used and used together, it makes perfect for date and time processing.

The requirement for this example is as follows. Know a date and time,

Example: 10:10:00

I want to add 5 months based on this date and return the processed date.

Result: 10:10:00 + 5 months equals 10:10:00

This requirement seems simple, but it is still a bit tricky, because PHP does not directly provide date time in the format of yyyy-mm-dd hh: ii: ss for addition and subtraction, so it can only be achieved through time stamps. The timestamp is the standard format for program conversion, accurate to seconds. PHP can convert multiple date formats to timestamps and convert timestamps back to various date formats. In combination with these two features, we roughly implement three steps, first, convert the original time to the timestamp, add or subtract it, and finally convert it back to the date format.

Of course, this is the implementation principle. Combining the date () and strtotime () functions of PHP functions, this is also the meaning. Please refer to the instance code.
Copy codeThe Code is as follows:
<? Php
* Date addition and subtraction method in PHP
* Qiongtai Old House
// Step 1, assume there is a time
$ A = '2017-04-25 10:10:00 ';
// Step 2, obtain the timestamp of this date
$ A_time = strtotime ($ );
// Step 3: Obtain the timestamp after adding five months
$ B _time = strtotime ('+ 5 month', $ a_time );
// Part 4: Convert the timestamp Back To The 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 ($ )));
Echo 'this is the date after adding five months '. $ B;
We will not detail the use of date () and strtotime () functions here. You can refer to my previous related function introduction articles or go to php.net to see the manual.

Php date addition and subtraction

Do not think that php is so stupid, the current time uses time (), according to your needs to come to the most direct way.
$ Time = time ();
For ($ I = 1; $ I <= 5; $ I ++ ){
$ S = getdate ($ time-$ I * 24*3600 );
$ Day [$ I] [mdy] = $ s [mday];
$ Day [$ I] [mon] = $ s [mon];
In this way, you will get the month and day of the past five days. replace-with +, that is, the month and day of the next five days.
However, I found that you do not know much about the timestamp definition.
In the next five days, time () + 5*24*3600,
The last five days are time ()-5*24*3600,
Why cannot I use a timestamp?

How to Implement the PHP date addition/subtraction countdown code in HTML

We recommend that you use javascript to write code. It is troublesome to use PHP to complete this process.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.