Method of calculating time differences in php and MySql

  • 2020-05-05 10:58:59
  • OfStack

Recently, when I was studying my love blog, I had to count the number of love days, which requires php to calculate according to the date of each day. Here are some ways to achieve this kind of date calculation:

(1) it would be easy if you had a database! If MSSQL could use triggers! Use the datediff() function to calculate the date difference! If it is MYSQL, then the calculation results calculated with the difference between the two date fields are saved in another numeric field! When called!

(2) if you don't have a database, you have to use php's time and date function completely!

The number of days from May 3, 1998 to June 5, 1999:
 
$startdate=mktime("0","0","0","5","3","1998"); $enddate=mktime("0","0","0","6","5","1999"); // The value obtained is from 1970-1-1 The total number of seconds to the parameter time turns out to be an integer . The following code is much easier to code  
$days=round(($enddate-$startdate)/3600/24) ; 
echo $days; 

$days is the number of days obtained; If the parameter in mktime() defaults, the current date is used, and the number of days since the date of borrowing can be calculated.

Finally, the calculation method of SQL:

DateDiff function
Description: returns the time interval between two dates.
Grammar:
 
DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear>) 

interval: choice. A string expression that indicates the time interval between date1 and date2 used to calculate. For values, see the Settings section.

date1, date2: required. Date expression. Two dates used to calculate.

firstdayofweek: optional. Specifies a constant for the first day of the week. If not specified, the default is Sunday. For values, see the Settings section.

firstweekofyear: optional. Specify a constant for the first week of the year. If not specified, the default is the week of January 1. For values, see the Settings section.

The interval parameter can have the following values:
yyyy (years), q (quarters), m (months), y (days of the year), d (days), w (days of the week), ww (weeks), h (hours), n (minutes), s (seconds)

The firstdayofweek parameter can have the following value:
(the following are: constant value description)
vbUseSystem 0 USES the locale language support (NLS) API setting.
vbSunday 1 Sunday (default)
vbMonday 2 Monday
vbTuesday 3 Tuesday
vbWednesday 4 Wednesday
vbThursday 5 Thursday
vbFriday 6 Friday
vbSaturday 7 Saturday
The firstweekofyear parameter can have the following values:
(the following are: constant value description)
vbUseSystem 0 USES the locale 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 of the New Year.

Note: the DateDiff function is used to determine the number of specified time intervals between two dates. For example, DateDiff can be used to calculate the number of days between two dates, or the number of weeks between that day and the last day of the year.

To calculate the number of days between date1 and date2, you can use either "days of the year" (" y ") or "days" (" d "). When interval is "days of the week" (" w "), DateDiff returns the number of weeks between the two dates. If date1 is a Monday, DateDiff counts the Monday before date2. This result contains date2 and not date1. If interval is "weeks" (" ww "), the DateDiff function returns the number of weeks between two dates in the calendar. The function calculates the number of Sundays between date1 and date2. If date2 is Sunday, DateDiff will calculate date2, but even if date1 is Sunday, date1 will not be calculated.

If date1 is later than date2, the DateDiff function returns a negative number.
The firstdayofweek parameter has an impact on the calculation using the "w" and "ww" interval symbols.

If date1 or date2 are date text, the specified year becomes a fixed part of the date. But if date1 or date2 are included in quotes (" ") and the year is omitted, the current year is inserted each time an date1 or date2 expression is evaluated in the code. This allows you to write program code for different years.

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

DatePart function

Description: returns the specified part of a given date.
Grammar:

DatePart(interval, date[, firstdayofweek[, firstweekofyear>) 


DatePart: the syntax of the function takes the following arguments:

interval: choice. A string expression that indicates the time interval to return. For values, see the Settings section.

date: choice. The date expression to evaluate.

firstdayof week: optional. Specifies a constant for the first day of the week. If not specified, the default is Sunday. For values, see the Settings section.

firstweekofyear: optional. Specify a constant for the first week of the year. If not specified, the default is the week of January 1. For values, see the Settings section.

The interval parameter can have the following value:
yyyy (year), q (quarter), m (month), y (days of the year), d (days), w (days of the week), ww (weeks), h (hours), n (minutes), s (seconds)

The firstdayofweek parameter can have the following value:
(the following are: constant value description)
vbUseSystem 0 USES the locale language support (NLS) API setting.
vbSunday 1 Sunday (default)
vbMonday 2 Monday
vbTuesday 3 Tuesday
vbWednesday 4 Wednesday
vbThursday 5 Thursday
vbFriday 6 Friday
vbSaturday 7 Saturday
The firstweekofyear parameter can have the following values:
(the following are: constant value description)
vbUseSystem 0 USES the locale language support (NLS) API setting.
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 of the New Year.

Description: the DatePart function is used to calculate a date and return the specified interval. For example, use DatePart to calculate the day of the week or the current time.

The firstdayofweek parameter affects the calculation using "w" and "ww" interval symbols.
If date is date text, the specified year becomes a fixed part of the date. But if date is included in quotes (" ") and the year is omitted, the current year is inserted each time the date expression is evaluated in the code. This way you can write program code for different years!

Related articles: