How to calculate the time difference in PHP and MySQL _php tutorial

Source: Internet
Author: User
Tags date1 time and date

A detailed explanation of how the time difference is calculated in PHP and MySQL


Calculating the time difference in PHP is sometimes a hassle! But as long as you've mastered the use of datetime functions, that's a simple thing to do.

Recently in the study of their love scarf to calculate the number of days of love, which requires PHP based on the daily date of calculation, the following talk about the implementation of this date calculation of several methods:

(1) If you have a database, it's easy! If MSSQL can use a trigger! Use a function DateDiff () that calculates the date difference. If it's MySQL, it will be saved in another numeric field with the calculation of the difference between the two date fields! Time to call!

(2) If there is no database, then you have to use the PHP time and date function completely!

The following are the main explanations:

Example: Calculate the number of days from May 3, 1998 to 1999-6-5:

The code is as follows:


$startdate =mktime ("0", "0", "0", "5", "3", "1998"); $enddate =mktime ("0", "0", "0", "6", "5", "1999"); The resulting value is the total number of seconds from 1970-1-1 to the parameter time result is an integer. Then the code is a lot more.
$days =round (($enddate-$startdate)/3600/24);
Echo $days;

Where $days is the number of days to get;

If the parameter in Mktime () is the default, it means that the current date is used, so that the number of days from the borrowing date is calculated.

Finally, let's talk about how SQL is calculated:

DateDiff function

Description: Returns a time interval between two dates.

Grammar:

DateDiff (interval, date1, date2 [, firstdayofweek[, firstweekofyear>)
Interval: Must be selected. A string expression that is used to calculate the time interval between Date1 and Date2. See the Settings section for values.

Date1, Date2: Required. The date expression. The two dates used for the calculation.

FirstDayOfWeek: Optional. Constant that specifies the first day of the week. If not specified, the default is Sunday. See the Settings section for values.

FirstWeekOfYear: Optional. A constant that specifies the first week of the year. If not specified, the default is the week in which January 1 is located. See the Settings section for values.

The interval parameter can have the following values:

YYYY (year)
Q (quarterly)
M (month)
Y (number of days in a year)
D (Sun)
W (number of days in a week)
WW (week)
H (Hours)
N (minutes)
S (sec)
The FirstDayOfWeek parameter can have the following values:

(The following are: Constant value description)

Vbusesystem 0 uses the regional language Support (NLS) API settings.
Vbsunday 1 Sunday (default)
Vbmonday 2 weeks A
Vbtuesday 3 Week II
Vbwednesday 4 week Three
Vbthursday 5 Thu
Vbfriday 6 Friday
Vbsaturday 7 Saturday
The FirstWeekOfYear parameter can have the following values:

(The following are: Constant value description)

Vbusesystem 0 uses the regional language Support (NLS) API settings.
VbFirstJan1 1 starts from the week of January 1 (default).
vbFirstFourDays 2 starts with the first week of at least four days in the new year.
Vbfirstfullweek 3 starts with the first full week in the new year.
Description: The DateDiff function is used to determine the number of specified time intervals that exist between two dates.

For example, you can use DateDiff to calculate the number of days that differ by two dates, or the day of the week between the last day of the year.

To calculate the number of days between Date1 and Date2, you can use the days of the year ("Y") or the "Day" ("D"). When interval is "Days of the Week" ("W"), DateDiff returns the number of weeks between two dates.

If Date1 is Monday, DateDiff calculates the number of Monday before Date2. This result contains date2 and does not contain date1.

If interval is "Week" ("WW"), the DateDiff function returns the number of weeks between two dates in the Calendar table. The function calculates the number of Sunday between Date1 and Date2.

If Date2 is Sunday, DateDiff calculates date2, but even if Date1 is Sunday, date1 is not counted.

If Date1 is later than Date2, the DateDiff function returns a negative number. The FirstDayOfWeek parameter has an effect on the calculations that use the "w" and "ww" interval symbols.

If Date1 or date2 is a date literal, the specified year becomes a fixed part of the date. However, if Date1 or date2 is included in quotation marks ("") and the year is omitted, the current year is inserted each time the Date1 or date2 expression is evaluated in the code. This allows you to write program code that applies to different years.

When the interval is "year" ("yyyy"), compare December 31 and the next year January 1, although actually only one day, DateDiff returns 1 indicates a difference of one year.

DatePart function

Description: Returns the specified part of the given date. Grammar:

DatePart (interval, date[, firstdayofweek[, firstweekofyear>)
DatePart: The syntax of a function has the following parameters:
Interval: Must be selected. A string expression that represents the interval of time to return. See the Settings section for values.
Date: Must be selected. The date expression to evaluate.
FirstDayOfWeek: Optional. Constant that specifies the first day of the week. If not specified, the default is Sunday. See the Settings section for values.
FirstWeekOfYear: Optional. A constant that specifies the first week of the year. If not specified, the default is the week in which January 1 is located. See the Settings section for values.
Where the interval parameter can have the following values: YYYY (year), Q (Quarter), M (month), Y (Days of the year), D (Day), W (Days of the Week), WW (weeks), H (Hours), N (minutes), s (seconds)

Where the FirstDayOfWeek parameter can have the following values:

(The following are: Constant value description)

Vbusesystem 0 uses the regional language Support (NLS) API settings.
Vbsunday 1 Sunday (default)
Vbmonday 2 weeks A
Vbtuesday 3 Week II
Vbwednesday 4 week Three
Vbthursday 5 Thu
Vbfriday 6 Friday
Vbsaturday 7 Saturday
The FirstWeekOfYear parameter can have the following values:

(The following are: Constant value description)

Vbusesystem 0 uses the regional language Support (NLS) API settings.
VbFirstJan1 1 starts from the week of January 1 (default).
vbFirstFourDays 2 starts with the first week of at least four days in the new year.
Vbfirstfullweek 3 starts with the first full week (not straddling) in the new Year.
Description: The DatePart function is used to calculate the date and return the specified time interval. For example, use DatePart to calculate the day of the week or the current time.

Where the FirstDayOfWeek parameter affects the calculation using the "W" and "ww" interval symbols.

If date is a day literal, the specified year becomes a fixed part of the date. However, if date is enclosed in quotation marks ("") and the year is omitted, the current year is inserted each time the date expression is evaluated in the code. This allows you to write program code that applies to different years!

The above mentioned is the whole content of this article, I hope to be able to master PHP to help you.

Please take a moment to share the article with your friends or leave a comment. We would be grateful for your support!

http://www.bkjia.com/PHPjc/975134.html www.bkjia.com true http://www.bkjia.com/PHPjc/975134.html techarticle How to calculate the time difference in PHP and mysql It is sometimes a hassle to calculate the time difference in PHP! But as long as you have the use of the date-and-times function, that's a simple thing to do. ...

  • Related Article

    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.