SoFunction
Updated on 2025-03-09

Detailed explanation of how to calculate time difference in php and MySql

Calculating time difference in php is sometimes a hassle! But as long as you master the usage of date and time functions, these will become simple.

Recently, when I was studying my love scarf, I had to calculate the number of days of love. This requires php to calculate based on the date of the day. Let’s talk about several methods to implement this date calculation:

(1) It's easy to have a database! If it's MSSQL, you can use a trigger! Use the function datediff() that specializes in calculating date difference! If it's MYSQL, save the calculation result of the difference between the two date fields in another numerical field! You can call it when you use it!

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

The following mainly explains:

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

Copy the codeThe code is as follows:

$startdate=mktime("0","0","0","5","3","1998"); $enddate=mktime("0","0","6","5","1999"); //The result is an integer from 1970-1-1 to the parameter time. Then the following code is much easier to write
$days=round(($enddate-$startdate)/3600/24) ;
echo $days;

where $days is the number of days obtained;

If the parameter in mktime() is defaulted, it means that the current date is used, so that the number of days from the date of the book to the present can be calculated.

Finally, let’s talk about SQL calculation method:

DateDiff function

Description: Returns the time interval between two dates.

grammar:

DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear>)
interval: Required. A string expression that represents the time interval used to calculate date1 and date2. For values, see the Settings section.

date1, date2: Required. Date expression. Two dates used for calculation.

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

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

The interval parameter can have the following values:

yyyy (year)
q (Quarterly)
m (month)
y (the number of days in one year)
d (Day)
w (days of a week)
ww (week)
h (hours)
n (min)
s (seconds)
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 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 Regional Language Support (NLS) API settings.
vbFirstJan1 1 starts from the week of January 1 (default).
vbFirstFourDays 2 begins 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 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 when two dates differ, or the number of weeks between the same day and the last day of the year.

To calculate the number of days between date1 and date2, you can use "Days of a Year" ("y") or "Days" ("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 Mondays before date2. This result contains date2 and not 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 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 effect on the calculation using the "w" and "ww" interval symbols.

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

When interval is "year" ("yyyy"), compare December 31 and January 1 of the following year. Although it is actually only one day apart, DateDiff returns 1 to indicate one year difference.

DatePart function

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

DatePart(interval, date[, firstdayofweek[, firstweekofyear>)
DatePart: The syntax of the function has the following parameters:
interval: Required. A string expression that indicates the time interval to be returned. For values, see the Settings section.
date: Required. The date expression to be calculated.
firstdayofweek: Optional. Specifies the constant for the first day of the week. If not specified, the default is Sunday. For values, see the Settings section.
firstweekofyear: Optional. Specify the constant for the first week of the year. If not specified, the default is the week of January 1st. For values, see the Settings section.
The interval parameter can have the following values: yyyy (year), q (quarter), m (month), y (days of one year), d (days), w (days of one week), ww (week), h (hours), n (minutes), s (seconds)

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 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 Regional Language Support (NLS) API settings.
vbFirstJan1 1 starts from the week of January 1 (default).
vbFirstFourDays 2 begins 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 (not across the year).
Description: The DatePart function is used to calculate a date and return the specified time interval. For example, use DatePart to calculate the day of the week or the current time.

The firstdayofweek parameter affects the calculation of using the "w" and "ww" interval symbols.

If date is a 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 every time the date expression is calculated in the code. This way you can write program code for different years!

The above is all about this article. I hope it will be helpful for everyone to master php proficiently.

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