Php date addition and subtraction operation implementation code

Source: Internet
Author: User
Tags echo date
In php, if we get another date by adding or subtracting a few days from a date, we will use two instances to indicate that the time is converted to the timestamp by strtotime and mktime and then processed, if you need it, check it out. Example: obtain 1 before January 1...

In php, if we get another date by adding or subtracting a few days from a date, we will use two instances to indicate that the time is converted to the timestamp by strtotime and mktime and then processed, if you need it, check it out.

Example: obtain the date of the previous day from January 1 ,.

The code is as follows:

// Convert a time point to a timestamp
$ Date = strtotime ('2017-5-1 ');
// Output the date before the day, minus the number of seconds of the day in the timestamp
Echo date ('Y-m-D', $ date-1*24*60*60 );
?>

Output: 2012-4-30

In addition, the time () function obtains the timestamp of the current date!

Let's look at another instance.

The code is as follows:

$ Date_1 = ""; // format: $ Date_1 = "23:29:14 ″;

$ Date_2 = "2009-10-1 ″;

$ 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 ";
?>

Time reference

The code is as follows:

Display format: year-month-day hour: minute: wonderful

Related time parameters:

A-"am" or "pm"
A-"AM" or "PM"
D-a few days, two digits. if there are less than two digits, add zero in front, for example, "01" to "31 ″
D-the day of the week, with three English letters, for example, "Fri"
F-month, full English name, for example, "January"
H-12 hours, for example, "01" to "12 ″
H-24 hours, for example, "00" to "23 ″
For hours in the g-12 hour format, less than two do not fill in the zero; for example: "1" to 12 ″
For hours in the G-24 hour system, less than two do not make up zero; for example, "0" to "23 ″
I-minutes; for example, "00" to "59 ″
J-a few days, two digits. if less than two digits are left blank, for example, "1" to "31 ″
L-the day of the week, full name in English; for example, "Friday"
M-month, two digits. if there are less than two digits, add zero in front. for example, "01" to "12 ″
N-month, two digits. if less than two digits are left blank, for example, "1" to "12 ″
M-month, with three English letters, for example, Jan"
S-seconds; for example, "00" to "59 ″
The end of the S-character is followed by an English sequence number with two English letters, such as "th" and "nd"
T-specifies the number of days in a month. for example, "28" to "31 ″
U-total seconds
W-number of weeks, for example, "0" (Sunday) to "6" (Saturday)
Y-year, four digits, for example, "1999 ″
Y-year, two digits, for example, "99 ″
Z-the day of the year. for example, "0" to "365 ″

You can freely set the displayed content, connection symbols, or display locations, such as date ("m-d H") or date ("dmY");?> And so on



Tutorial address:

Reprinted! But please include the article address ^

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.