Example of a date addition and subtraction method in PHP, PHP date Example _php tutorial

Source: Internet
Author: User

Example of a date addition and subtraction method in PHP, PHP date Example


Almost all programmers who are involved in program development encounter time processing problems, and PHP development is the same, fortunately PHP provides a lot of information about date-time functions. As long as these functions are used in conjunction with each other, date and time processing makes perfect.

This is the example that needs to be told today. Knowing a date and time,

Example: 2012-04-25 10:10:00

I want to add 5 months to this date and time and return to the date of processing.

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

This requirement seems simple, but still a bit tricky, because PHP does not directly provide YYYY-MM-DD hh:ii:ss such a format date time to add and subtract, so it can only be done by timestamp. Timestamps are the standard format for program conversions, accurate to seconds. PHP can convert a variety of date formats to timestamps, but also the time stamp can be converted back to various date formats, combined with these two features we have three steps to achieve the approximate step, first convert the original time to timestamp, then add and subtract, and finally converted back to the date format.

Of course, this is the implementation of the principle, combined with the PHP function date () and Strtotime () two functions to achieve a general meaning, see the example code
Copy the Code code as follows:
<?php
/**
* Date Addition and subtraction method in PHP
* The old house of Qiong Tai
*/
The first step is to assume that there is a time
$a = ' 2012-04-25 10:10:00 ';

Step two, get the timestamp for this date
$a _time = strtotime ($a);

Step three, get the timestamp after five months plus
$b _time = Strtotime (' +5 Month ', $a _time);

Fourth, convert the timestamp back to the date format
$b = Date (' y-m-d h:i:s ', $b _time);
Echo ' This is a date added for five months '. $b;

If you think the above code is too long, you can do it one line.
$b = Date (' y-m-d h:i:s ', strtotime (' + '. $time. ' Month ', strtotime ($a)));
Echo ' This is a date added for five months '. $b;
?>
The use of the date () function with the Strtotime () function is not described in detail here. Children's shoes need to see my previous related functions of the article or to php.net to see the manual can be.


PHP date plus subtraction problem

Do not think of PHP so stupid, the current time (), according to your needs 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];
}
This way you get the last 5 days of the month and the day, will-replace + is the next 5 days of the month and day.
However, I find that you are not quite familiar with the definition of timestamp.
In the next five days, it is time () +5*24*3600,
In the last five days, it was time () -5*24*3600,
Why can't I use time stamps?

PHP date plus minus countdown code how to implement in HTML

It is recommended to write code in JavaScript language, which is cumbersome to do with PHP.

Specific countdown code you can search the Web under, similar code there are many.

http://www.bkjia.com/PHPjc/867245.html www.bkjia.com true http://www.bkjia.com/PHPjc/867245.html techarticle Example of a date addition and subtraction method in PHP, PHP date example almost all programmers involved in program development encounter time processing problems, PHP development is the same, fortunately PHP provides a lot about the day ...

  • Related Article

    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.