Home  >  Article  >  Backend Development  >  How to calculate the time difference in php and MySql_PHP tutorial

How to calculate the time difference in php and MySql_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:30:16885browse

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

(1) If It is very easy to have a database! If it is MSSQL, you can use triggers! Just use the function datediff() that specially calculates the date difference! If it is MYSQL, then use the difference between two date fields to calculate the calculation result and save it in another numeric field. Medium! Just call it when you need it!

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

The following is the main explanation: Example: Calculate May 3, 1998 Number of days to 1999-6-5:

Copy code The code is as follows:

$startdate=mktime("0" ,"0","0","5","3","1998"); $enddate=mktime("0","0","0","6","5","1999" ); //The obtained 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 much easier to compile
$days=round(($enddate-$startdate) /3600/24);
echo $days;

$days is the number of days obtained; if the parameter in mktime() is default, it means using the current date, so that the calculation can be done from The number of days since the borrowing date.

Finally, let’s talk about the SQL calculation method:

DateDiff function
Description: Returns the time interval between two dates.
Syntax:
Copy code The code is as follows:

DateDiff(interval, date1, date2 [, firstdayofweek[, firstweekofyear> ;)

interval: Required. A string expression used to calculate the time interval between date1 and date2. See the Settings section for values.

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

firstdayofweek: Optional. A constant that specifies the first day of the week. If not specified, it defaults to Sunday. See the Settings section for values.

firstweekofyear: Optional. Constant that specifies the first week of the year. If not specified, it defaults to the week of January 1st. See the Settings section for values.

interval parameter can have the following values:
yyyy (year), q (quarter), m (month), y (number of days in a year), d (day), w (number of days in a week) , ww (week), h (hour), n (minute), s (second)

firstdayofweek parameter can have the following values:
(the following are: constant value description)
vbUseSystem 0 Use 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
firstweekofyear Parameters can have the following values:
(The following are: constant value description)
vbUseSystem 0 Use the Regional Language Support (NLS) API setting.
vbFirstJan1 1 Starts with the week of January 1st (default).
vbFirstFourDays 2 starts with the first week of the new year having at least four days.
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 between two dates, or the number of weeks between today and the last day of the year.

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

If date1 is later than date2, the DateDiff function returns a negative number.
The firstdayofweek parameter affects calculations using the "w" and "ww" separator symbols.

If date1 or date2 is a date literal, the specified year becomes a fixed part of the date. But if date1 or date2 is enclosed in quotation marks (" ") and the year is omitted, the current year will be inserted every time the date1 or date2 expression is evaluated in the code. This makes it possible to write program code that works for different years.

When interval is "year" ("yyyy"), compare December 31st with January 1st of the next year. Although the difference is actually only one day, DateDiff returns 1 to indicate a difference of one year.

DatePart function

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

Copy code The code is as follows:
DatePart(interval, date[, firstdayofweek[, firstweekofyear>)


DatePart: The syntax of the function has the following parameters:

interval: required. A string expression representing the time interval to be returned. See the Settings section for values.

date: required. The date expression to evaluate.

firstdayof week: Optional. A constant that specifies the first day of the week. If not specified, it defaults to Sunday. See the Settings section for values.

firstweekofyear: Optional. Constant that specifies the first week of the year. If not specified, it defaults to the week of January 1st. See the Settings section for values.

interval parameter can have the following values:
yyyy (year), q (quarter), m (month), y (number of days in a year), d (day), w (number of days in a week) , ww (week), h (hour), n (minute), s (second)

firstdayofweek parameter can have the following values:
(the following are: constant value description)
vbUseSystem 0 Use 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
firstweekofyear Parameters can have the following values:
(The following are: constant value description)
vbUseSystem 0 Use the Regional Language Support (NLS) API setting.
vbFirstJan1 1 Starts with the week of January 1st (default).
vbFirstFourDays 2 starts with the first week of the new year having at least four days.
vbFirstFullWeek 3 starts with the first full week of the new year (not across years).

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

The firstdayofweek argument affects calculations using the "w" and "ww" separator symbols.
If date is a date literal, the specified year becomes a fixed part of the date. But if date is enclosed in quotation marks (" ") and the year is omitted, the current year will be inserted every time the date expression is evaluated in the code. This way you can write program code that works for different years!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323277.htmlTechArticleRecently, when I was studying my love for scarves, I had to calculate the number of days in love. This requires PHP to calculate based on the daily date. , let’s talk about several methods to implement this kind of date calculation: (1) If there is...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn