Method of calculating time difference in PHP and MySQL _php tips

Source: Internet
Author: User
Tags date1 php and php and mysql time and date time interval

Calculating the time difference in PHP is sometimes a hassle! But as long as you have the use of the date and the function, that's a simple thing to do.

Recently, when I studied my favorite scarf, I counted the number of days in love, which requires PHP to calculate on a daily basis, and here's a few ways to implement this date calculation:

(1) If there is a database is very easy! If the MSSQL can use a trigger! DateDiff () with a function that calculates the date difference. If it's MySQL, use the two-date field's difference calculation to save the result in another numeric field! Time to call!

(2) If there is no database, it must be completely with the PHP time and date function!

The following main notes:

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

Copy Code code 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 the result is an integer. Then the following code is so much more.
$days =round (($enddate-$startdate)/3600/24);
Echo $days;

Among them $days is the number of days to be obtained;

If the parameter in Mktime () defaults, that means the current date is used, so that you can calculate the number of days from the date the book was borrowed.

Finally, let's say how the SQL is calculated:

DateDiff function

Description: Returns the time interval between two dates.

Grammar:

DateDiff (interval, date1, date2 [, firstdayofweek[, firstweekofyear>)
Interval: Must choose. A string expression that is used to calculate the time interval between Date1 and Date2. For numeric values, see the "Settings" section.

Date1, Date2: Must choose. An expression of a date. Two dates used for the calculation.

FirstDayOfWeek: Optional. Specifies the constant for the first day of the week. If not specified, the default is Sunday. For numeric values, see the "Settings" section.

FirstWeekOfYear: Optional. Specifies a constant for the first week of the year. If not specified, the default is the week of January 1. For numeric values, see the "Settings" section.

The interval parameter can have the following values:

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

(The following are: Constant numerical description)

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

(The following are: Constant numerical description)

Vbusesystem 0 uses the regional language Support (NLS) API settings.
VbFirstJan1 1 begins on the week of January 1 (default).
vbFirstFourDays 2 begins with the first week of at least four days in the new year.
Vbfirstfullweek 3 begins with the first full week of 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 between two date differences, or the day to the last day of the year.

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

If the date1 is Monday, then 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. function calculates the number of Sunday between Date1 and Date2.

If the date2 is Sunday, DateDiff will calculate the date2, but even if Date1 is Sunday, it will not calculate date1.

If Date1 is later than Date2, the DateDiff function returns a negative number. The FirstDayOfWeek parameter affects computations 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 the 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 works for different years.

When interval is "year" ("yyyy"), comparing the January 1 of December 31 and the following year, although the actual difference is only one day, DateDiff returns 1 to indicate a difference of a year.

DatePart function

Description: Returns the specified portion of a given date. Grammar:

DatePart (interval, date[, firstdayofweek[, firstweekofyear>)
DatePart: The syntax of a function has the following parameters:
Interval: Must choose. A string expression that represents the time interval to return. For numeric values, see the "Settings" section.
Date: Must be selected. The date expression to evaluate.
FirstDayOfWeek: Optional. Specifies the constant for the first day of the week. If not specified, the default is Sunday. For numeric values, see the "Settings" section.
FirstWeekOfYear: Optional. Specifies a constant for the first week of the year. If not specified, the default is the week of January 1. For numeric values, see the "Settings" section.
Where the interval parameter can have the following values: YYYY (year), Q (Quarter), M (month), Y (Day of year), D (Day), W (Day of Week), WW (week), H (hour), N (minutes), S (sec)

Where the FirstDayOfWeek parameter can have the following values:

(The following are: Constant numerical description)

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

(The following are: Constant numerical description)

Vbusesystem 0 uses the regional language Support (NLS) API settings.
VbFirstJan1 1 begins on the week of January 1 (default).
vbFirstFourDays 2 begins with the first week of at least four days in the new year.
Vbfirstfullweek 3 begins 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 date 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 works for different years!

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

Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

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.