Home php教程 php手册 PHP中几个常用的时间应用方式

PHP中几个常用的时间应用方式

Jun 13, 2016 am 11:13 AM
php several exist application Way time yes standard use of set up default

在PHP中默认设置是标准的格林威治时间(零时区),所以如果要获得本地时间必须修改PHP的默认时区(八时区,除非您是外国的朋友,我想很少吧)。

PHP系统时区设置

修改PHP系统时区的两种方式:

1、修改php.ini文件中的设置,找到[date]下的“;date.timezone=”,将该项修改为date.timezone=Asia/Hong_Kong (PRC 中国时间),然后重新启动Apache服务器。

2、在应用程序中,使用时间日期函数之前添加“date_default_timezone_set(“Asia/Hong_Kong”)”函数

修改php.ini文件后获取本地时间:

php代码:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li>
<span class="func">echo</span><span> “现在是北京时间:”.</span><span class="func">date</span><span>(“Y-m-d H:i:s”).”<p>”;  </p></span>
</li>
<li class="alt"><span>?> </span></li>
</ol>
Copy after login

显示结果:

现在是北京时间:2010-11-30 20:50:03 (与本地时间相符)

php.ini修改后的代码:

<ol class="dp-c">
<li class="alt"><span><span>[</span><span class="func">Date</span><span>]  </span></span></li>
<li>
<span>; Defines the </span><span class="keyword">default</span><span> timezone used by the </span><span class="func">date</span><span> functions  </span>
</li>
<li class="alt">
<span>; http:</span><span class="comment">//php.net/date.timezone </span><span> </span>
</li>
<li>
<span class="func">date</span><span>.timezone = PRC </span>
</li>
</ol>
Copy after login

Zero希望大家按照上面的方法去设置本地时区,要注意的是修改的php.ini文件必须是当前服务器所加载的php.ini文件。

PHP中比较两个时间的大小

在日常生活中我们要经常比较时间的早晚,对于我们来说判断时间的大小很简单。但是时间的比较不只是单纯的数字大小的比较,因此相对来说还是比较复杂。那么在PHP中通过什么方式来比较两个时间的大小呢?如果认真学习了上篇博文《从34到35,PHP时间戳》中的内容,我想这个问题也不会很棘手。

要比较两个时间的大小,我们需要将时间转化为时间戳格式,然后再进行比较这是最常用的方法。

常用到的函数是:strtotime()

语法格式:strtotime(time,now)

如果time是绝对时间,则now参数不起作用。

如果time是相对时间,则相对应的参数则对应函数就是now来提供,如果没有提供now参数,那么相对应的时间就是当前的本地时间。

实例:比较两个绝对时间的大小

代码:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li>
<span class="vars">$zero1</span><span>=</span><span class="func">date</span><span>(“Y-m-d H:i:s”);  </span>
</li>
<li class="alt">
<span class="vars">$zero2</span><span>=”2010-11-29 21:07:00″;  </span>
</li>
<li>
<span class="func">echo</span><span> “zero1的时间为:”.</span><span class="vars">$zero1</span><span>.”<br>”;  </span>
</li>
<li class="alt">
<span class="func">echo</span><span> “zero2的时间为:”.</span><span class="vars">$zero2</span><span>.”<br>”;  </span>
</li>
<li>
<span class="keyword">if</span><span>(</span><span class="func">strtotime</span><span>(</span><span class="vars">$zero1</span><span>)<span class="func">strtotime</span><span>(</span><span class="vars">$zero2</span><span>)){  </span></span>
</li>
<li class="alt">
<span class="func">echo</span><span> “zero1早于zero2″;  </span>
</li>
<li>
<span>}</span><span class="keyword">else</span><span>{  </span>
</li>
<li class="alt">
<span class="func">echo</span><span> “zero2早于zero1″;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>?> </span></li>
</ol>
Copy after login

输出结果:

zero1的时间为:2010-11-30 21:12:55

zero2的时间为:2010-11-29 21:07:00

zero2早于zero1

注:可以根据实例发散思维

计算两个日期的差值

奥运会倒计时,亚运会倒计时,生日倒计时这些倒计时都可以通过计算两个日期的差值来实现,同样需要用到strottime()函数。
实现倒计时需要将两个时间的差值整数化,需要用到函数ceil()

ceil()函数的作用是求不小于给定实数的最小整数

实例:倒计时小程序

实例代码:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li>
<span class="vars">$zero1</span><span>=</span><span class="func">strtotime</span><span> (</span><span class="func">date</span><span>(“Y-m-d H:i:s”)); </span><span class="comment">//当前时间 </span><span> </span>
</li>
<li class="alt">
<span class="vars">$zero2</span><span>=</span><span class="func">strtotime</span><span> (“2011-2-03 24:00:00″); </span><span class="comment">//过年时间 </span><span> </span>
</li>
<li>
<span class="vars">$guonian</span><span>=</span><span class="func">ceil</span><span>((</span><span class="vars">$zero2</span><span>-</span><span class="vars">$zero1</span><span>)/86400); </span><span class="comment">//60s*60min*24h </span><span> </span>
</li>
<li class="alt">
<span class="func">echo</span><span> “离过年还有<strong></strong></span><span class="vars">$guonian</span><span>天!”;  </span>
</li>
<li><span>?> </span></li>
</ol>
Copy after login

输出结果:离过年还有66天!

计算脚本的运行时间

在打开很多网页时都会有一个脚本的运行时间,百度搜索时也会出现搜索用时这些元素。那么是通过什么来实现的呢?

计算脚本运行时间需要用到microtime()函数,该函数的作用是返回当前时间戳和微秒数。返回格式为msec sec的字符串,其中sec是当前的UNIX时间戳,msec 为微秒数。

语法格式:microtime(void)

运算原理:分别记录脚本运行前后的时间戳,并计算两个时间戳的差值

实例:计算上个实例中的脚本运行时间

实例代码:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li>
<span class="keyword">function</span><span> run_time()  </span>
</li>
<li class="alt"><span>{  </span></li>
<li>
<span>list(</span><span class="vars">$msec</span><span>, </span><span class="vars">$sec</span><span>) = </span><span class="func">explode</span><span>(” “, microtime());  </span>
</li>
<li class="alt">
<span class="keyword">return</span><span> ((float)</span><span class="vars">$msec</span><span> + (float)</span><span class="vars">$sec</span><span>);  </span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span class="vars">$start_time</span><span>=run_time();  </span>
</li>
<li>
<span class="vars">$zero1</span><span>=</span><span class="func">strtotime</span><span> (</span><span class="func">date</span><span>(“Y-m-d H:i:s”)); </span><span class="comment">//当前时间 </span><span> </span>
</li>
<li class="alt">
<span class="vars">$zero2</span><span>=</span><span class="func">strtotime</span><span> (“2011-2-03 24:00:00″); </span><span class="comment">//过年时间 </span><span> </span>
</li>
<li>
<span class="vars">$guonian</span><span>=</span><span class="func">ceil</span><span>((</span><span class="vars">$zero2</span><span>-</span><span class="vars">$zero1</span><span>)/86400); </span><span class="comment">//60s*60min*24h </span><span> </span>
</li>
<li class="alt">
<span class="func">echo</span><span> “离过年还有<strong></strong></span><span class="vars">$guonian</span><span>天!”;  </span>
</li>
<li>
<span class="vars">$end_time</span><span>=run_time();  </span>
</li>
<li class="alt"><span>?>  </span></li>
<li><span><p> </p></span></li>
</ol>
Copy after login

网页加载时间:

运行结果:离过年还有66天! 网页加载时间:0.00011682510375977秒

explode()函数和list()函数将在以后的学习中详细介绍!

好了到这里Zero的PHP时间与日期学习就告一段落了,接下来将学习关于PHP核心技术方面的内容。欢迎各位博友持续关注!


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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles