How does php add a day at a certain time? One hour? Time addition and subtraction

Source: Internet
Author: User
How does php add a day at a certain time? One hour? Time addition and subtraction Date_default_timezone_set ('prc'); // Default time zone
Echo "today:", date ("Y-m-d", time ()),"
";
Echo "today:", date ("Y-m-d", strtotime ("18 Jun 2008 ")),"
";
Echo "yesterday:", date ("Y-m-d", strtotime ("-1 day ")),"
";
Echo "tomorrow:", date ("Y-m-d", strtotime ("+ 1 day ")),"
";
Echo "one week later:", date ("Y-m-d", strtotime ("+ 1 week ")),"
";
Echo "one week, two days, four hours, two seconds later:", date ("Y-m-d G: H: s ", strtotime ("+ 1 week 2 days 4 hours 2 seconds ")),"
";
Echo "next Thursday:", date ("Y-m-d", strtotime ("next Thursday ")),"
";
Echo "last Monday:". date ("Y-m-d", strtotime ("last Monday "))."
";
Echo "a month ago:". date ("Y-m-d", strtotime ("last month "))."
";
Echo "one month later:". date ("Y-m-d", strtotime ("+ 1 month "))."
";
Echo "Ten years later:". date ("Y-m-d", strtotime ("+ 10 year "))."
";
?>


One day + n days:

Strtotime can accept the second parameter, type timestamp, for the specified date

Echo date ('Y-m-D', strtotime ("+ 1 day", strtotime ('2017-11-01 '), "\ n ";

Echo "today:", date ('Y-m-d H: I: s '),"
";
Echo "tomorrow:", date ('Y-m-d H: I: S', strtotime ('+ 1 DAY '));
?>
Current time output from the previous row and tomorrow time output from the next row

Here + 1 day
You can change parameter 1 to any desired number of days or change it to year, month, hour, minute, and second)
For example
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.

Another article:

Common date functions in php [date addition and subtraction, difference between two dates, date conversion time cut] the following code is some common date processing functions. you can add or subtract dates of two time periods, the difference between the two dates and the date conversion time cut.

Echo date ('Y-m-D', strtotime ('+ 1 d', strtotime ('2017-07-08 ')));

// Date-days addition function echo date

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

The following code is a common date processing function, which can be used to add or subtract two dates, the difference between two dates, and the date conversion time cut.

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

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

$ D = "10:19:00 ";
Echo date ("Y-m-d", strtotime ("$ d + 1 day"); // date plus days function

Function dateToTime ($ d) // converts a date to a time heap
{
$ Year = (int) substr ("$ d",); // Obtain the year

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

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

Return mktime (0, 0, 0, $ month, $ day, $ year );
}

$ Date_1 = "2009-07-08 ";

Echo $ Date_1 + 1;

$ Date_2 = "2009-06-08 ";
$ Date_List_a1 = explode ("-", $ Date_1 );

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

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

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

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

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.