Several common Time application methods in PHP _ PHP Tutorial

Source: Internet
Author: User
Several common Time application methods in PHP. In PHP, the default setting is the standard Greenwich Mean Time (zero time zone). therefore, to obtain the local time, you must modify the default time zone of PHP (eight time zone, unless you are a foreign friend, I want to set the standard Greenwich Mean Time (zero time zone) by default in PHP, so if you want to get the local time, you must modify the default PHP time zone (eight time zone, unless you are a foreign friend, I don't think so ).

PHP System time zone settings

You can modify the time zone of the PHP System in either of the following ways:

1. modify php. in the INI file, locate "; date." in [date. timezone = ", 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.

Modify the php. ini file and obtain the local time:

Php code:

 
 
  1. Echo "now Beijing time:". date ("Y-m-d H: I: s ")."

    ";

  2. ?>

Display result:

It is now Beijing Time: 20:50:03 (consistent with the local time)

Php. ini modified code:

 
 
  1. [Date]
  2. ; Defines the default timezone used by the date functions
  3. ; http://php.net/date.timezone
  4. date.timezone = PRC

Zero wants you to set the local time zone according to the above method. Note that the modified php. ini file must be the php. ini file loaded by the current server.

Compare the two time sizes in PHP

In daily life, we often compare the time sooner or later. it is very easy for us to determine the size of time. However, the comparison of time is not just a comparison of numbers, so it is relatively complicated. In PHP, how does one compare the two time ranges? If you have carefully studied the content in the previous blog article "from 34 to 35, PHP timestamp", I think this problem will not be very tricky.

To compare the two time values, we need to convert the time to the timestamp format and then compare them. this is the most common method.

Commonly used functions: strtotime ()

Syntax format: strtotime (time, now)

If time is absolute time, the now parameter does not work.

If time is relative time, the corresponding parameter is provided by the corresponding function now. if the now parameter is not provided, the corresponding time is the current local time.

Instance: compare the two absolute time values

Code:

 
 
  1. $ Zero1 = date ("Y-m-d H: I: s ");
  2. $ Zero2 = "21:07:00 ″;
  3. Echo "zero1 Time:". $ zero1 ."
    ";
  4. Echo "zero2 Time:". $ zero2 ."
    ";
  5. If (strtotime ($ zero1)
  6. Echo "zero1 earlier than zero2 ″;
  7. } Else {
  8. Echo "zero2 earlier than zero1 ″;
  9. }
  10. ?>

Output result:

Zero1 Time: 21:12:55

Zero2 Time: 21:07:00

Zero2 is earlier than zero1

Note: Divergent thinking can be performed based on instances.

Calculate the difference between two dates

Olympics countdown, Asian Games countdown, and birthday countdown can all be achieved by calculating the difference between the two dates. the strottime () function is also required.
To implement the countdown, the difference between the two time values must be integer, and the ceil () function must be used ()

The ceil () function is used to obtain the minimum integer not less than the given real number.

Example: Countdown applet

Instance code:

 
 
  1. $ Zero1 = strtotime (date ("Y-m-d H: I: s"); // The current time
  2. $ Zero2 = strtotime ("24:00:00"); // Chinese new year's time
  3. $ Guonian = ceil ($ zero2-$ zero1)/86400); // 60 s * 60 min * 24 h
  4. Echo$ GuonianDay !";
  5. ?>

Output: there are still 66 days before the Chinese New Year!

Computing script running time

When opening many web pages, there will be a script running time, and these elements will also appear during Baidu search. So what is implemented through?

The microtime () function is used to calculate the script running time. This function returns the current timestamp and the number of microseconds. The string in the format of msec sec, where sec is the current UNIX timestamp and msec is the microseconds.

Syntax format: microtime (void)

Operation Principle: record the timestamp before and after running the script, and calculate the difference between the two timestamps.

Instance: calculates the script running time in the previous instance.

Instance code:

 
 
  1. Function run_time ()
  2. {
  3. List ($ msec, $ sec) = explode ("", microtime ());
  4. Return (float) $ msec + (float) $ sec );
  5. }
  6. $ Start_time = run_time ();
  7. $ Zero1 = strtotime (date ("Y-m-d H: I: s"); // The current time
  8. $ Zero2 = strtotime ("24:00:00"); // Chinese new year's time
  9. $ Guonian = ceil ($ zero2-$ zero1)/86400); // 60 s * 60 min * 24 h
  10. Echo$ GuonianDay !";
  11. $ End_time = run_time ();
  12. ?>

Webpage loading time: Seconds

Running result: there are still 66 days before the Chinese New Year! Webpage loading time: 0.00011682510375977 seconds

The explode () and list () functions will be described in detail in the future!

So far, Zero's PHP time and date have come to an end. next we will learn about the core technologies of PHP. Thank you for your continued attention!


Except (zero time zone), so if you want to obtain the local time, you must modify the default time zone of PHP (eight time zone, unless you are a foreign friend, I want...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.