Home  >  Article  >  Backend Development  >  PHP code sharing for date addition and subtraction calculations

PHP code sharing for date addition and subtraction calculations

小云云
小云云Original
2018-03-01 13:40:081449browse

PHP standard date format

date("Y-m-d H:i:s");

PHP Simple date addition and subtraction calculation


##1

2

3

4

5

6

7

8

9

10

11

12

13

14


#

date_default_timezone_set('PRC'); //Default time zone

echo "Today:",date( "Y-m-d",time()),"\n";

echo "Today:",date( "Y-m-d",strtotime("18 june 2008")), "\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 "One week, two days, four hours and two seconds later:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "\n";

echo "Next Thursday:",date("Y-m-d",strtotime("next Thursday")),"\n";

echo "Last Monday:".date("Y-m-d",strtotime("last Monday"))."\n";

echo "One month ago:".date("Y-m-d",strtotime("last month"))."\n";

echo "One month later:".date("Y-m-d",strtotime("+1 month"))."\n";

echo "Ten years later:".date("Y-m-d",strtotime("+10 year"))."\n";

?>


Run result:

1

2

3

4

5

6

7

8

9

10

11


Today: 2017-11-06

Today: 2008-06-18

##Yesterday: 2017-11- 05

##Tomorrow: 2017-11-07

One week later: 2017-11-13

One week, two days, four hours and two seconds later: 2017-11-15 22:22:42

Next Thursday: 2017-11-09

Last Monday:2017-10-30

One month ago:2017-10-06

One month later :2017-12-06

Ten years later: 2027-11-06


PHP Advanced Date Addition and Subtraction Calculation

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15


#

//Default time zone

​​ date_default_timezone_set('PRC');

    echo "Today: ", date( 'Y-m-d H:i:s'), "\n";        // Output the current time

    echo "Tomorrow: ", date( 'Y-m-d H:i:s', strtotime('+1 day')), "\n";           //Output tomorrow’s time

                         

//strtotime can accept the second parameter, the type timestamp is the specified date

    echo date('Y-m-d H:i:s', strtotime ("+1 day", strtotime('2017-11-11'))), "\n";

// +1 day here can modify parameter 1 to whatever you want The number

// day can also be changed to year (year) month (month) hour (hour) minute (minute) second (second)

    echo date('Y-m-d H:i:s', strtotime("+1 day +1 hour +1 minute")), "\n";

?>

Run results:

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 of Date and Time Difference


##1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35


##

##​

//Default time zone

date_default_timezone_set('PRC');

echo date("Y-m-d H:i:s"), "\ n";

// Date and day addition function

    echo date('Y-m-d', strtotime('+1 day', strtotime('2016-09-12'))), "\n\n";

 

    echo strtotime('2016-09-12'), "\n";

    echo date("Y-m-d", '1473609600'), "\n";

echo date("Y-m-d", ' 1573609600'), "\n";

// Date and day addition function

    $d = "2016-09-12 10:12:20";

echo date("Y-m-d", strtotime ("$d +1 day")), "\n\n";

//Convert date to time

                                                                                                                       #$year=((int)substr("$d"

,0, 4)); //Obtained year

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

        $day=( (int)substr("$d",8,2)); //Get the number Number

        return mktime(0,0,0,$month,$day,$year);

    }

    echo date2time("2016-09-12"), "\n\n";

 

    $date_1="2066-09-12";

    $date_2="2016-09-12";

    $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 "$date_2 -> $date_1 difference: $Days days", "\n";

?>

Running results:


1

2

3

4

5

6

7

8

9

10

11


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: 18262 days

Related recommendations:

Oracle time and date addition and subtraction calculation

PHP date and time

PHP date and time functions

The above is the detailed content of PHP code sharing for date addition and subtraction calculations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn