PHP date plus subtraction calculation

Source: Internet
Author: User
PHP Standard date format

Date ("y-m-d h:i:s");

PHP simple date plus and minus calculation

<?php    date_default_timezone_set (' PRC ');//default time zone    echo "Today:", Date ("Y-m-d", Time ()), "\ n";    echo "Today:", Date ("Y-m-d", Strtotime ("June")), "\ n";    echo "Yesterday:", Date ("Y-m-d", Strtotime ("1 day"), "\ n";    echo "Tomorrow:", Date ("Y-m-d", Strtotime ("+1 Day"), "\ n";    echo "One week later:", Date ("Y-m-d", Strtotime ("+1 Week")), "\ n";    echo "2 days a week four hours two seconds after:", Date ("Y-m-d g:h:s", Strtotime ("+1 Week 2 Day 4 hours 2 Seconds"), "\ n";    echo "Next week Four:", Date ("Y-m-d", Strtotime ("next Thursday")), "\ n";    echo "Previous Monday:". Date ("Y-m-d", Strtotime ("Last Monday")). " \ n ";    echo "One months ago:". Date ("Y-m-d", Strtotime ("Last month"). " \ n ";    echo "One months later:". Date ("Y-m-d", Strtotime ("+1 Month")). " \ n ";    echo "Ten years later:". Date ("Y-m-d", Strtotime ("+10 Year")). " \ n ";? >

Operation Result:

Today: 2017-11-06

Today: 2008-06-18

Yesterday: 2017-11-05

Tomorrow: 2017-11-07

After one week: 2017-11-13

2 days a week four hours two seconds later: 2017-11-15 22:22:42

Next Thursday: 2017-11-09

Last Monday: 2017-10-30

One months ago: 2017-10-06

One months later: 2017-12-06

Ten years later: 2027-11-06

PHP advanced date Plus and minus calculation

<?php    //default time zone    date_default_timezone_set (' PRC ');         echo "Today:", Date (' y-m-d h:i:s '), "\ n";       Output Current time    echo "Tomorrow:", Date (' y-m-d h:i:s ', Strtotime (' +1 Day ')), "\ n";        Output tomorrow time         //strtotime can accept the second parameter, type timestamp for the specified date    echo date (' y-m-d h:i:s ', Strtotime ("+1 Day", Strtotime (' 2017-11-11 ')), "\ n";              Here +1 day can modify parameter 1 for any desired number/day     can also be changed to year (years) month (month) hour (hours) minute (min) second (seconds)    echo date (' y-m-d h:i:s ', Strtotime ("+1 day +1 hour +1 minute")), "\ n";? >

Operation Result:

Today: 2017-11-06 18:58:04
Tomorrow: 2017-11-07 18:58:04
2017-11-12 00:00:00
2017-11-07 19:59:04

PHP Advanced Calculation Date Time difference

<?php//default time zone Date_default_timezone_set (' PRC ');     echo Date ("Y-m-d h:i:s"), "\ n";     Date days Add function echo date (' y-m-d ', Strtotime (' +1 Day ', Strtotime (' 2016-09-12 '))), "\ n";    echo strtotime (' 2016-09-12 '), "\ n";    echo Date ("y-m-d", ' 1473609600 '), "\ n";     echo Date ("y-m-d", ' 1573609600 '), "\ n";    Date number Add function $d = "2016-09-12 10:12:20";       echo Date ("Y-m-d", Strtotime ("$d +1 Day"), "\ n";  Convert date to Time intercept function Date2time ($d) {$year = ((int) substr ("$d", 0,4)); Year $month = ((int) substr ("$d", 5,2));   Gets the Month $day = ((int) substr ("$d", 8,2));    Gets the number of return Mktime (0,0,0, $month, $day, $year);     } Echo Date2time ("2016-09-12"), "\ n";    $date _1= "2066-09-12";    $date _2= "2016-09-12";    $Date _list_a1=explode ("-", $date _1);    $Date _list_a2=explode ("-", $date _2);    $d 1=mktime (0,0,0, $Date _list_a1[1], $Date _list_a1[2], $Date _list_a1[0]);    $d 2=mktime (0,0,0, $Date _list_a2[1], $Date _list_a2[2], $Date _list_a2[0]); $Days =round (($d 1-$d 2)/3600/24); echo "$date _2 $date _1 difference: $Days days", "\ n";? >

Operation Result: 2017-11-06 19:13:02

2016-09-13

1473609600

2016-09-12

2019-11-13

2016-09-13

1473609600

2066-09-12-2016-09-12 difference: 18,262 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.