The default setting in PHP is the standard GMT (zero zone), so if you want to get local time you must modify the default time zone of PHP (eight time zone, unless you are a foreign friend, I think very little).
PHP System time zone settings
There are two ways to modify the PHP system time zone:
1. Modify the settings in the php.ini file, locate ";d ate.timezone=" under [Date], modify the entry to Date.timezone=asia/hong_kong (PRC time in China), and then restart the Apache server.
2. In the application, add the "Date_default_timezone_set (" Asia/hong_kong ") function before using the time-date function
To get local time after modifying the php.ini file:
PHP Code:
-
- Echo "Now is Beijing time:". Date ("y-m-d h:i:s"). "
”;
Show Results:
It's Beijing time: 2010-11-30 20:50:03 (in accordance with local time)
PHP.ini the modified code:
- [Date]
- Defaultdate functions
- ; http
- Date
Zero hope that you follow the above method to set the local time zone, to note that the modified php.ini file must be the current server loaded php.ini file.
Compare two-time sizes in PHP
In our daily life, we often compare time with each other, and it is very simple for us to judge the size of time. But the comparison of time is not just a comparison of the number size, so it is relatively complex. So how do you compare the size of two times in PHP? If I had studied the contents of the last post, from 34 to 35,php time stamp, I don't think it would be a tricky question.
To compare the size of two times, we need to convert the time into a timestamp format and then compare it to the most common method.
The functions commonly used are: Strtotime ()
Syntax format: strtotime (Time,now)
If time is absolute, the now parameter does not work.
If time is relative, then the corresponding function is now provided, and if it is not provided, then the corresponding time is the current local time.
Example: Compare the size of two absolute times
Code:
-
- $zero 1 = Date ("y-m-d h:i:s");
- $zero 2 = "2010-11-29 21:07:00″;
- Echo "The Time for Zero1 is:". $zero 1 . "
”;
- Echo "The Time for Zero2 is:". $zero 2 . "
”;
- if (strtotime($zero 1) <strtotime($zero 2 )){
- Echo "Zero1 earlier than Zero2″;
- }else{
- Echo "Zero2 earlier than Zero1″;
- }
- ?>
Output Result:
Zero1 Time: 2010-11-30 21:12:55
Zero2 Time: 2010-11-29 21:07:00
Zero2 earlier than Zero1
Note: The thought can be divergent according to the instance
Calculates the difference between two dates
Countdown to the Olympic Games, the countdown to the Asian Games, and the countdown for birthdays can be achieved by calculating the difference of two dates, as well as using the Strottime () function.
The implementation of the countdown needs to be two time difference integer, need to use the function ceil ()
The function of the ceil () function is to find the smallest integer not less than the given real number
Example: Countdown applet
Instance code:
-
- $zero 1 = Strtotime (date("y-m-d h:i:s")); //Current time
- $zero 2 = Strtotime ("2011-2-03 24:00:00″); //New Year time
- $guonian = Ceil (($zero 2-$zero 1)/86400); //60s*60min*24h
- Echo "The New Year is still $guonian Day! ”;
- ?>
Output: 66 days before the New Year!
Calculate the run time of a script
When you open a lot of web pages will have a script run time, Baidu Search will also appear when the search for these elements. So what is the realization of it?
The Microtime () function is used to calculate the run time of the script, which is the function of returning the current timestamp and the number of microseconds. Returns a string formatted as msec sec, where the SEC is the current Unix timestamp and msec is the number of microseconds.
Syntax format: microtime (void)
Operating principle: Record the time stamp before and after the script, and calculate the difference of two timestamp
Example: Calculating the script run time in the last instance
Instance code:
-
- function run_time ()
- {
- list ($msec, $sec) = Explode ("", Microtime ());
- return (float)$msec + (float)$sec);
- }
- $start _time =run_time ();
- $zero 1 = Strtotime (date("y-m-d h:i:s")); //Current time
- $zero 2 = Strtotime ("2011-2-03 24:00:00″); //New Year time
- $guonian = Ceil (($zero 2-$zero 1)/86400); //60s*60min*24h
- Echo "The New Year is still $guonian Day! ”;
- $end _time =run_time ();
- ?>
Page load time: seconds
Running result: 66 days before the New Year! Page load time: 0.00011682510375977 seconds
The explode () function and the list () function will be described in detail later in the study!
Okay, here we go. Zero php time and date learning is over, and then you'll learn about the core technologies of PHP. Welcome to Bo Friends to continue to pay attention!
http://www.bkjia.com/PHPjc/445817.html www.bkjia.com true http://www.bkjia.com/PHPjc/445817.html techarticle the default setting in PHP is the standard GMT (zero zone), so if you want to get local time you must modify the default time zone of PHP (eight time zone, unless you are a foreign friend, I think ...)