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:
-
- Echo "now Beijing time:". date ("Y-m-d H: I: s ")."
";
- ?>
Display result:
It is now Beijing Time: 20:50:03 (consistent with the local time)
Php. ini modified code:
- [Date]
- ; Defines the default timezone used by the date functions
- ; http://php.net/date.timezone
- 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:
-
- $ Zero1 = date ("Y-m-d H: I: s ");
- $ Zero2 = "21:07:00 ″;
- Echo "zero1 Time:". $ zero1 ."
";
- Echo "zero2 Time:". $ zero2 ."
";
- If (strtotime ($ zero1)
- Echo "zero1 earlier than zero2 ″;
- } Else {
- Echo "zero2 earlier than zero1 ″;
- }
- ?>
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:
-
- $ Zero1 = strtotime (date ("Y-m-d H: I: s"); // The current time
- $ Zero2 = strtotime ("24:00:00"); // Chinese new year's time
- $ Guonian = ceil ($ zero2-$ zero1)/86400); // 60 s * 60 min * 24 h
- Echo$ GuonianDay !";
- ?>
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:
-
- Function run_time ()
- {
- List ($ msec, $ sec) = explode ("", microtime ());
- Return (float) $ msec + (float) $ sec );
- }
- $ Start_time = run_time ();
- $ Zero1 = strtotime (date ("Y-m-d H: I: s"); // The current time
- $ Zero2 = strtotime ("24:00:00"); // Chinese new year's time
- $ Guonian = ceil ($ zero2-$ zero1)/86400); // 60 s * 60 min * 24 h
- Echo$ GuonianDay !";
- $ End_time = run_time ();
- ?>
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...