第十章 日期与时间
学习要点:
1.PHP 日期和时间库
使用PHP 编程时,与你遇到的大多数其他类型的数据相比,日期和时间有很大不同。
因为日期和时间没有明确的结构,并且日期的计算和表示也很麻烦。在PHP 中,日期和时
间函数库是PHP 语言的一个核心部分。
时间戳是自1970 年1 月1 日(00:00:00 GMT)以来的秒数。它也被称为Unix 时间
戳(Unix Timestamp)。Unix 时间戳(Unix timestamp),或称Unix 时间(Unix time)、POSIX 时
间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970 年01 月01 日00 时00
分00 秒起至现在的总秒数。Unix 时间戳不仅被使用在Unix 系统、类Unix 系统中,也在许
多其他操作系统中被广泛采用。例如(1184557366 表示2007-07-16 03:42:46 )
一.PHP日期和时间库
验证日期:checkdate()函数能够很好地验证日期,提供的日期如果有效,则返回true,
否则返回false。
<?<span php </span><span if</span> (<span checkdate</span>(2,29,2007<span )) { </span><span echo</span> '日期合法'<span ; } </span><span else</span><span { </span><span echo</span> '日期不合法'<span ; } </span>?>
格式化时间和日期:date()函数返回根据预定义指令格式化时间和日期的字符串形式。
所有格式参数,可以参考手册。
<?<span php </span><span echo</span> <span date</span>('Y-m-d H:i:sa'); <span //</span><span 直接输入日期和时间</span> <span echo</span> <span date</span>('今天的日期和时间为:Y/m/d H:i:sa'); <span //</span><span 可以插入无关的字符串</span> ?>
查看更多时间信息:gettimeofday()函数返回与当前时间有关的元素所组成的一个关联数
组。
<?<span php </span><span print_r</span>(<span gettimeofday</span>()); <span //</span><span 可以传入一个真(1)</span> ?>
将时间戳转换成友好的值:getdate()函数接受一个时间戳,并返回一个由其各部分组成
的关联数组。如果不给参数,那么返回当前的时间和日期。
<?<span php </span><span print_r</span>(<span getdate</span>(1184557366<span )); </span>?>
获取当前的时间戳:time()函数可以获取当前的时间戳,并且可以通过设置时间戳的值。
<?<span php </span><span echo</span> <span date</span>('Y-m-d H:i:s',<span time</span>()+(7*24*60*60<span )); </span>?>
获取特定的时间戳:mktime()函数可以生成给定日期时间的时间戳。
<?<span php </span><span echo</span> <span mktime</span>(14,14,14,11,11,2007<span ); </span><span echo</span> <span date</span>('Y-m-d H:i:s',<span mktime</span>(14,14,14,11,11,2007<span )); </span>?>
计算时间差
<?<span php </span><span $now</span> = <span time</span><span (); </span><span $taxday</span> = <span mktime</span>(0,0,0,7,17,2010<span ); </span><span echo</span> <span round</span>((<span $taxday</span> - <span $now</span>)/60/60<span ); </span>?>
将日期转换成时间戳:strtotime()将人可读的日期转换为Unix 时间戳。
<?<span php </span><span echo</span> <span strtotime</span>('2007-10-31 14:31:33'<span ); </span>?>
计算时间差
<?<span php </span><span echo</span> (<span strtotime</span>('2007-10-31 14:31:33') - <span strtotime</span>('2007-10-31 11:31:33'))/60/60<span ; </span>?>
获取当前文件最后修改时间:getlastmod()可以得到当前文件最后修改时间的时间戳。
<?<span php </span><span echo</span> <span date</span>('Y-m-d H:i:s',<span getlastmod</span><span ()); </span>?>
设置时区和GMT/UTC:
修改php.ini 文件中的设置,找到[date]下的;date.timezone = 选项,将该项修改为
date.timezone=Asia/Shanghai,然后重新启动apache 服务器。
putenv()函数可以设置当前的默认时区。
<?<span php </span><span putenv</span>('TZ=Asia/Shanghai'<span ); </span><span echo</span> <span date</span>('Y-m-d H:i:s'<span ); </span>?>
date_default_timezone_set()可以设置当前的默认时区。
date_default_timezone_get()可以获取当前的默认时区。
<?<span php date_default_timezone_set(</span>'Asia/Shanghai'<span ); </span><span echo</span> <span date</span>('Y-m-d H:i:s'<span ); </span>?>
取得本地时间localtime()函数可以取得本地时间数据,然后返回一个数组。
<?<span php date_default_timezone_set(</span>'Asia/Shanghai'<span ); </span><span print_r</span>(<span localtime</span><span ()); </span><span print_r</span>(<span localtime</span>(<span time</span>(), <span true</span><span )); </span>?>
计算页面脚本运行时间:microtime()函数,该函数返回当前UNIX 时间戳和微秒数。返
回格式为msec sec 的字符串,其中sec 是当前的UNIX 时间戳,msec 为微秒数。
<?<span php </span><span function</span><span fntime() { </span><span list</span>(<span $msec</span>, <span $sec</span>) = <span explode</span>(' ', <span microtime</span><span ()); </span><span return</span> <span $msec</span>+<span $sec</span><span ; } </span><span $start_time</span> =<span fntime(); </span><span for</span>(<span $i</span>=0;<span $i</span><1000000;<span $i</span>++<span ) { } </span><span $end_time</span> =<span fntime(); </span><span echo</span> <span round</span>(<span $end_time</span> - <span $start_time</span>,4<span ); </span>?>
注:文章出自李炎恢PHP视频教程,本文仅限交流使用,不得用于商业用途,否则后果自负。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The combination of Vue.js and ASP.NET provides tips and suggestions for performance optimization and expansion of web applications. With the rapid development of web applications, performance optimization has become an indispensable and important task for developers. As a popular front-end framework, Vue.js combined with ASP.NET can help us achieve better performance optimization and expansion. This article will introduce some tips and suggestions, and provide some code examples. 1. Reduce HTTP requests The number of HTTP requests directly affects the loading speed of web applications. pass

Translator | Reviewed by Chen Jun | Chonglou In the 1990s, when people mentioned software programming, it usually meant choosing an editor, checking the code into the CVS or SVN code base, and then compiling the code into an executable file. Corresponding integrated development environments (IDEs) such as Eclipse and Visual Studio can integrate programming, development, documentation, construction, testing, deployment and other steps into a complete software development life cycle (SDLC), thus improving the work of developers. efficiency. In recent years, popular cloud computing and DevSecOps automation tools have improved developers' comprehensive capabilities, making it easier for more enterprises to develop, deploy and maintain software applications. Today, generative AI is the next generation development

How to correctly use and optimize the MySQL connection pool in ASP.NET programs? Introduction: MySQL is a widely used database management system that features high performance, reliability, and ease of use. In ASP.NET development, using MySQL database for data storage is a common requirement. In order to improve the efficiency and performance of database connections, we need to correctly use and optimize the MySQL connection pool. This article will introduce how to correctly use and optimize the MySQL connection pool in ASP.NET programs.

How to reconnect to MySQL in ASP.NET program? In ASP.NET development, it is very common to use the MySQL database. However, due to network or database server reasons, the database connection may sometimes be interrupted or time out. In this case, in order to ensure the stability and reliability of the program, we need to re-establish the connection after the connection is disconnected. This article will introduce how to reconnect MySQL connections in ASP.NET programs. To reference the necessary namespaces first, reference them at the head of the code file

The combination of Vue.js and ASP.NET enables the development and deployment of enterprise-level applications. In today's rapidly developing Internet technology field, the development and deployment of enterprise-level applications has become more and more important. Vue.js and ASP.NET are two technologies widely used in front-end and back-end development. Combining them can bring many advantages to the development and deployment of enterprise-level applications. This article will introduce how to use Vue.js and ASP.NET to develop and deploy enterprise-level applications through code examples. First, we need to install

How to correctly configure and use MySQL connection pool in ASP.NET program? With the development of the Internet and the increase in data volume, the demand for database access and connections is also increasing. In order to improve the performance and stability of the database, connection pooling has become an essential technology. This article mainly introduces how to correctly configure and use the MySQL connection pool in ASP.NET programs to improve the efficiency and response speed of the database. 1. The concept and function of connection pooling. Connection pooling is a technology that reuses database connections. At the beginning of the program,

How to correctly use and optimize the transaction performance of MySQL connection pool in ASP.NET programs? In ASP.NET programs, database transactions are a very important part. Transactions ensure the consistency and integrity of the database while also providing better performance. When using a MySQL database, it is essential to use connection pools to manage connection resources and optimize performance. First, let us briefly understand the concept of MySQL connection pool. The connection pool is a buffer pool of a group of connections. By pre-initializing a certain number of

The built-in objects in ASP.NET include "Request", "Response", "Session", "Server", "Application", "HttpContext", "Cache", "Trace", "Cookie" and "Server.MapPath": 1. Request, indicating the HTTP request issued by the client; 2. Response: indicating the HTTP response returned by the web server to the client, etc.
