Home Backend Development PHP Tutorial Code example of method to calculate time difference in php and MySql

Code example of method to calculate time difference in php and MySql

Jul 10, 2017 am 09:42 AM
mysql php Time difference

在php中计算时间差有时候是件麻烦的事!不过只要你掌握了日期时间函数的用法那这些也就变的简单了。

最近在研究自己爱围脖的时候就要计算到恋爱天数,这需要php根据每天的日期进行计算,下面就来谈谈实现这种日期计算的几种方法:

(1) 如果有数据库就很容易了!若是MSSQL可以使用触发器!用专门计算日期差的函数datediff()便可! 若是MYSQL那就用两个日期字段的差值计算的计算结果保存在另一个数值型字段中!用时调用便可!

(2)如果没有数据库,那就得完全用php的时间日期函数!

下面主要说明之: 例:计算1998年5月3日到1999-6-5的天数:

 代码如下:

$startdate=mktime("0","0","0","5","3","1998"); $enddate=mktime("0","0","0","6","5","1999"); //所得到的值为从1970-1-1到参数时间的总秒数结果是整数.那么下面的代码就好编多了 
$days=round(($enddate-$startdate)/3600/24) ; 
echo $days;
Copy after login

$days为得到的天数; 若mktime()中的参数缺省,那表示使用当前日期,这样便可计算从借书日期至今的天数。

最后说一下SQL的计算方法:

DateDiff 函数
描述:返回两个日期之间的时间间隔。
语法:

代码如下:

DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear>)
Copy after login

interval: 必选。字符串表达式,表示用于计算 date1 和 date2 之间的时间间隔。有关数值,请参阅“设置”部分。

date1, date2: 必选。日期表达式。用于计算的两个日期。

firstdayofweek: 可选。指定星期中第一天的常数。如果没有指定,则默认为星期日。有关数值,请参阅“设置”部分。

firstweekofyear: 可选。指定一年中第一周的常数。如果没有指定,则默认为 1 月 1 日所在的星期。有关数值,请参阅“设置”部分。

interval 参数可以有以下值:
yyyy (年) 、q (季度) 、m (月) 、y (一年的日数) 、d (日) 、w (一周的日数) 、ww (周) 、h (小时) 、n (分钟) 、s (秒)

firstdayofweek 参数可以有以下值:
(以下分别为:常数 值 描述)
vbUseSystem 0 使用区域语言支持 (NLS) API 设置。
vbSunday 1 星期日(默认)
vbMonday 2 星期一
vbTuesday 3 星期二
vbWednesday 4 星期三
vbThursday 5 星期四
vbFriday 6 星期五
vbSaturday 7 星期六
firstweekofyear 参数可以有以下值:
(以下分别为:常数 值 描述)
vbUseSystem 0 使用区域语言支持 (NLS) API 设置。
vbFirstJan1 1 由 1 月 1 日所在的星期开始(默认)。
vbFirstFourDays 2 由在新年中至少有四天的第一周开始。
vbFirstFullWeek 3 由在新的一年中第一个完整的周开始。

说明:DateDiff 函数用于判断在两个日期之间存在的指定时间间隔的数目。例如可以使用 DateDiff 计算两个日期相差的天数,或者当天到当年最后一天之间的星期数。

要计算 date1 和 date2 相差的天数,可以使用“一年的日数”(“y”)或“日”(“d”)。当 interval 为“一周的日数”(“w”)时,DateDiff 返回两个日期之间的星期数。如果 date1 是星期一,则 DateDiff 计算到 date2 之前星期一的数目。此结果包含 date2 而不包含 date1。如果 interval 是“周”(“ww”),则 DateDiff 函数返回日历表中两个日期之间的星期数。函数计算 date1 和 date2 之间星期日的数目。如果 date2 是星期日,DateDiff 将计算 date2,但即使 date1 是星期日,也不会计算 date1。

如果 date1 晚于 date2,则 DateDiff 函数返回负数。
firstdayofweek 参数会对使用“w”和“ww”间隔符号的计算产生影响。

如果 date1 或 date2 是日期文字,则指定的年度会成为日期的固定部分。但是如果 date1 或 date2 被包括在引号 (” “) 中并且省略年份,则在代码中每次计算 date1 或 date2 表达式时,将插入当前年份。这样就可以编写适用于不同年份的程序代码。

在 interval 为“年”(“yyyy”)时,比较 12 月 31 日和来年的 1 月 1 日,虽然实际上只相差一天,DateDiff 返回 1 表示相差一个年份。

DatePart 函数

描述:返回给定日期的指定部分。
语法:

代码如下:

DatePart(interval, date[, firstdayofweek[, firstweekofyear>)
Copy after login

DatePart: 函数的语法有以下参数:

interval: 必选。字符串表达式,表示要返回的时间间隔。有关数值,请参阅“设置”部分。

date: 必选。要计算的日期表达式。

firstdayof week: 可选。指定星期中的第一天的常数。如果没有指定,则默认为星期日。有关数值,请参阅“设置”部分。

firstweekofyear: 可选。指定一年中第一周的常数。如果没有指定,则默认为 1 月 1 日所在的星期。有关数值,请参阅“设置”部分。

interval 参数可以有以下值:
yyyy (年) 、q (季度) 、m (月) 、y (一年的日数) 、d (日) 、w (一周的日数) 、ww (周) 、h (小时) 、n (分钟) 、s (秒)

firstdayofweek 参数可以有以下值:
(以下分别为:常数 值 描述)
vbUseSystem 0 使用区域语言支持 (NLS) API 设置。
vbSunday 1 星期日(默认)
vbMonday 2 星期一
vbTuesday 3 星期二
vbWednesday 4 星期三
vbThursday 5 星期四
vbFriday 6 星期五
vbSaturday 7 星期六
firstweekofyear 参数可以有以下值:
(以下分别为:常数 值 描述)
vbUseSystem 0 使用区域语言支持 (NLS) API 设置。
vbFirstJan1 1 由 1 月 1 日所在的星期开始(默认)。
vbFirstFourDays 2 由在新年中至少有四天的第一周开始。
vbFirstFullWeek 3 由在新的一年中第一个完整的周(不跨年度)开始。

说明:DatePart 函数用于计算日期并返回指定的时间间隔。例如使用 DatePart 计算某一天是星期几或当前的时间。

firstdayofweek 参数会影响使用“w”和“ww”间隔符号的计算。
如果 date 是日期文字,则指定的年度会成为日期的固定部分。但是如果 date 被包含在引号 (” “) 中,并且省略年份,则在代码中每次计算 date 表达式时,将插入当前年份。这样就可以编写适用于不同年份的程序代码!

The above is the detailed content of Code example of method to calculate time difference in php and MySql. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles