Home  >  Article  >  Backend Development  >  Several commonly used time application methods in PHP_PHP tutorial

Several commonly used time application methods in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 10:56:56917browse

The default setting in PHP is standard Greenwich Time (zero time zone), so if you want to get the local time, you must modify the default time zone of PHP (eight time zone, unless you are a foreign friend, I think there are very few Bar).

PHP system time zone settings

Two ways to modify the PHP system time zone:

1. Modify the settings in the php.ini file , find ";date.timezone=" under [date], change this item to date.timezone=Asia/Hong_Kong (PRC China time), and then restart the Apache server.

2. In the application, add the "date_default_timezone_set("Asia/Hong_Kong")" function before using the time and date function

Get the local time after modifying the php.ini file:

php code:

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

Display result:

Now is Beijing time: 2010-11-30 20:50:03 (consistent with local time)

The modified code of 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>

Zero hopes that you can set the local time zone according to the above method. It should be noted that the modified php.ini file must be the php.ini file loaded by the current server. .

Comparing the size of two times in PHP

In daily life, we often need to compare the time early and late. It is very difficult for us to judge the time. Simple. However, the comparison of time is not just a comparison of simple numbers, so it is relatively complicated. So how to compare the size of two times in PHP? If you carefully studied the content in the previous blog post "From 34 to 35, PHP Timestamp", I think this problem will not be very difficult.

To compare the size of two times, we need to convert the time into timestamp format and then compare. This is the most common method.

Commonly used functions are: strtotime()

Syntax format: strtotime(time,now)

If time is an absolute time, the now parameter has no effect.

If time is a relative time, the corresponding parameter is provided by the corresponding function now. If no now parameter is provided, the corresponding time is the current local time.

Example: Compare the size of two absolute times

Code:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="vars">$zero1</span><span>=</span><span class="func">date</span><span>(&ldquo;Y-m-d H:i:s&rdquo;);  </span></li><li class="alt"><span class="vars">$zero2</span><span>=&rdquo;2010-11-29 21:07:00&Prime;;  </span></li><li><span class="func">echo</span><span> &ldquo;zero1的时间为:&rdquo;.</span><span class="vars">$zero1</span><span>.&rdquo;<br>”;  </span></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><span class="func">strtotime</span><span>(</span><span class="vars">$zero2</span><span>)){  </span></li><li class="alt"><span class="func">echo</span><span> &ldquo;zero1早于zero2&Prime;;  </span></li><li><span>}</span><span class="keyword">else</span><span>{  </span></li><li class="alt"><span class="func">echo</span><span> &ldquo;zero2早于zero1&Prime;;  </span></li><li><span>}  </span></li><li class="alt"><span>?> </span>
</li>
</ol>

Output result:

The time of zero1 is: 2010-11- 30 21:12:55

The time of zero2 is: 2010-11-29 21:07:00

zero2 is earlier than zero1

Note: You can think divergently based on examples

Calculate the difference between two dates

Olympic countdown, Asian Games countdown, birthday countdown, these countdowns can all be calculated by calculating the difference between two dates Implementation also requires the use of the strottime() function.
To implement the countdown, you need to integer the difference between the two times. You need to use the function ceil()

The function of the ceil() function is to find the smallest integer that is not less than a given real number

Example: Countdown applet

Example code:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="vars">$zero1</span><span>=</span><span class="func">strtotime</span><span> (</span><span class="func">date</span><span>(&ldquo;Y-m-d H:i:s&rdquo;)); </span><span class="comment">//当前时间 </span><span> </span></li><li class="alt"><span class="vars">$zero2</span><span>=</span><span class="func">strtotime</span><span> (&ldquo;2011-2-03 24:00:00&Prime;); </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> &ldquo;离过年还有<strong></span><span class="vars">$guonian</span><span></strong>天!”;  </span></span></li>
<li><span>?> </span></li>
</ol>

Output result: There are 66 days until the Chinese New Year!

Calculate the running time of the script

When opening many web pages, there will be a script running time, and these search time elements will also appear when searching on Baidu. So what is achieved?

Calculating the script running time requires the microtime() function, which returns the current timestamp and microseconds. Returns a string in the format msec sec, where sec is the current UNIX timestamp and msec is the number of microseconds.

Syntax format: microtime (void)

Operation principle: Record the timestamps before and after the script is run, and calculate the difference between the two timestamps

Example: Calculation Script running time in each instance

Instance code:

<ol class="dp-c">
<li class="alt"><span><span><?php  </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>(&rdquo; &ldquo;, 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>(&ldquo;Y-m-d H:i:s&rdquo;)); </span><span class="comment">//当前时间 </span><span> </span></li><li class="alt"><span class="vars">$zero2</span><span>=</span><span class="func">strtotime</span><span> (&ldquo;2011-2-03 24:00:00&Prime;); </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> &ldquo;离过年还有<strong></span><span class="vars">$guonian</span><span></strong>天!”;  </span></span></li>
<li>
<span class="vars">$end_time</span><span>=run_time();  </span>
</li>
<li class="alt"><span>?>  </span></li>
<li><span><p> </span></li>
</ol>

Web page loading time: Seconds

Running result: There are still 66 days until the Chinese New Year! Web page loading time: 0.00011682510375977 seconds

The explode() function and list() function will be introduced in detail in future studies!

Well, the learning of Zero’s PHP time and date has come to an end. Next, we will learn about the core technology of PHP. Welcome fellow bloggers to continue to pay attention!


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445817.htmlTechArticleThe default setting in PHP is standard Greenwich Time (zero time zone), so if you want to get the local time, you must Modify PHP's default time zone (eight time zones, unless you are a foreign friend, I want...
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