Php date addition and subtraction processing function example

Source: Internet
Author: User
Php date addition and subtraction processing function example

  1. Echo "today:", date ('Y-m-d H: I: s '),"
    ";
  2. Echo "tomorrow:", date ('Y-m-d H: I: S', strtotime ('+ 1 DAY '));
  3. ?>

Current time output from the previous row and tomorrow time output from the next row

Here, you can change the value of parameter 1 to any desired number of days, which can also be changed to year, month, hour, minute ), second (seconds) example:

  1. Date ('Y-m-d H: I: S', strtotime ("+ 1 day + 1 hour + 1 minute ");

Can be freely combined to achieve any output time. Note: This method is intended for trial use after January 1, 1970, that is, the applicable scope of the timestamp.

Common date functions in php [date addition and subtraction, difference between two dates, date conversion time cut]

For example, some commonly used date processing functions can be added or subtracted from two dates, the difference between two dates, and the date conversion time cut.

  1. Echo date ('Y-m-D', strtotime ('+ 1 d', strtotime ('1970-07-08'); // date plus functions

  2. Echo date ("Y-m-d", '20140901 ');

  3. Echo'
    ';
  4. Echo date ("Y-m-d", '20140901 ');
  5. Die ();

  6. $ D = "10:19:00 ";

  7. Echo date ("Y-m-d", strtotime ("$ d + 1 day"); // date plus days function

  8. Function dateToTime ($ d) // converts a date to a time heap

  9. {
  10. $ Year = (int) substr ("$ d",); // Obtain the year

  11. $ Month = (int) substr ("$ d", 5, 2); // Obtain the month

  12. $ Day = (int) substr ("$ d", 8, 2); // Obtain the number of digits

  13. Return mktime (0, 0, 0, $ month, $ day, $ year );

  14. }

  15. /*

  16. The following function calculates the difference between two dates.

  17. */

  18. $ Date_1 = "2009-07-08 ";

  19. Echo $ Date_1 + 1;

  20. $ Date_2 = "2009-06-08 ";

  21. $ Date_List_a1 = explode ("-", $ Date_1 );

  22. $ Date_List_a2 = explode ("-", $ Date_2 );

  23. $ D1 = mktime (0, 0, 0, $ Date_List_a1 [1], $ Date_List_a1 [2], $ Date_List_a1 [0]);

  24. $ D2 = mktime (0, 0, 0, $ Date_List_a2 [1], $ Date_List_a2 [2], $ Date_List_a2 [0]);

  25. $ Days = round ($ d1-$ d2)/3600/24 );

  26. Echo "the difference between the two dates is $ Days ";

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.