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.