Date addition and subtraction method in PHP example _php instance

Source: Internet
Author: User
Tags php example

Almost all programmers involved in program development encounter time processing problems, PHP development is the same, fortunately PHP provides a lot about date and time functions. As long as the use of these functions, with the use of date and time processing on practice makes perfect.

The demand for this example, which is to be told Today, is this. Knowing a certain date and time,

such as: 2012-04-25 10:10:00

I'm going to add 5 months on the basis of this date time and return the processed date.

Results: 2012-04-25 10:10:00 plus 5 months equals 2012-09-25 10:10:00

This demand seems simple, but still a bit tricky, because PHP does not provide direct yyyy-mm-dd hh:ii:ss in such a format date time to add and subtract, so only through the timestamp to achieve. A timestamp is a standard format for program conversions, accurate to seconds. PHP can convert a variety of date formats to time stamps, but also to convert the timestamp back to various date formats, combined with these two characteristics of our general implementation of the three steps, the original time into a timestamp, and then add and subtract, and finally converted back to date format.

Of course, this is the implementation principle, combining the PHP function date () and Strtotime () two functions to achieve roughly also this meaning, see the instance code

Copy Code code as follows:

<?php
/**
* Date Add and subtract method in PHP
* Jones Tai Old House
*/
The first step is to assume that there is a time
$a = ' 2012-04-25 10:10:00 ';

The second step is to get the time stamp of this date
$a _time = strtotime ($a);

Step three, get a five-month time stamp.
$b _time = Strtotime (' +5 Month ', $a _time);

Part fourth, convert the timestamp back to date format
$b = Date (' y-m-d h:i:s ', $b _time);
Echo ' This was added five months after the date '. $b;

If you think the above code is too long, you can handle it.
$b = Date (' y-m-d h:i:s ', strtotime (' + '. $time. ') Month ', strtotime ($a));
Echo ' This was added five months after the date '. $b;
?>

The use of the date () function and the Strtotime () function is not described in detail here. Need the children's shoes can see my previous related function introduction article or go to php.net to see the manual can be.

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.