Heim > Backend-Entwicklung > PHP-Tutorial > php开发之时间跟日期的应用

php开发之时间跟日期的应用

WBOY
Freigeben: 2016-06-13 12:16:54
Original
1017 Leute haben es durchsucht

php开发之时间和日期的应用

1,比较两个日期的大小
比较两个日期的大小,首先是将日期转换为时间戳,然后对时间戳进行比较。
具体示例代码如下:

<code class=" hljs xml"><span class="php"><span class="hljs-preprocessor"><?php</span><span class="hljs-variable">$time1</span> =date(<span class="hljs-string">"Y-m-d H:i:s"</span>);<span class="hljs-variable">$time2</span> =<span class="hljs-string">"2009-12-9 4:40:12"</span>;<span class="hljs-keyword">echo</span> <span class="hljs-string">"时间变量\$time1 的时间为:"</span>.<span class="hljs-variable">$time1</span>.<span class="hljs-string">"\n"</span>;<span class="hljs-keyword">echo</span> <span class="hljs-string">"时间变量\$time2 的时间为:"</span>.<span class="hljs-variable">$time2</span>.<span class="hljs-string">"\n"</span>;<span class="hljs-keyword">if</span>(Strtotime(<span class="hljs-variable">$time1</span>)-strtotime(<span class="hljs-variable">$time2</span>)<<span class="hljs-number">0</span>){    <span class="hljs-keyword">echo</span> <span class="hljs-string">"\$time1早于\$time2"</span>;}<span class="hljs-keyword">else</span>{    <span class="hljs-keyword">echo</span> <span class="hljs-string">"\$time2早于\$time1"</span>;}<span class="hljs-preprocessor">?></span></span></code>
Nach dem Login kopieren

运行结果如下:
这里写图片描述

2,实现页面脚本运行的时间
这里主要使用microtime()函数来实现,该函数返回当前UNIX时间戳和微秒数。返回格式为msec sec的字符串。其中sec为当前的UNIX时间戳,msec为微秒数。
语法格式如下:
string microtime(void)

示例代码如下:

<code class=" hljs xml"><span class="php"><span class="hljs-preprocessor"><?php</span><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">run_time</span><span class="hljs-params">()</span>{</span>    <span class="hljs-keyword">list</span>(<span class="hljs-variable">$msec</span>,<span class="hljs-variable">$sec</span>) =explode(<span class="hljs-string">" "</span>,microtime());  <span class="hljs-comment">//使用explode()函数返回两个变量</span>    <span class="hljs-keyword">return</span>((float)<span class="hljs-variable">$msec</span> + (float)<span class="hljs-variable">$sec</span>);          <span class="hljs-comment">//返回两个变量的和</span>}<span class="hljs-variable">$start</span> =run_time();  <span class="hljs-comment">//第一次运行run_time()函数</span><span class="hljs-keyword">for</span>(<span class="hljs-variable">$i</span>=<span class="hljs-number">0</span>;<span class="hljs-variable">$i</span><<span class="hljs-number">100000</span>;<span class="hljs-variable">$i</span>++){}<span class="hljs-variable">$end</span> =run_time();    <span class="hljs-comment">//再一次运行run_time()函数</span><span class="hljs-keyword">echo</span> <span class="hljs-string">"运行的时间:"</span>.(<span class="hljs-variable">$end</span>-<span class="hljs-variable">$start</span>).<span class="hljs-string">"\n"</span>;<span class="hljs-preprocessor">?></span></span></code>
Nach dem Login kopieren

这里写图片描述

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage