PHP _php Tutorials For a few common time application methods

Source: Internet
Author: User
Tags time and date vars
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:

 
  
  
  1. 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:

 
  
  
  1. [Date]
  2. Defaultdate functions
  3. ; http
  4. 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:

 
 
  1. $zero 1 = Date ("y-m-d h:i:s");
  2. $zero 2 = "2010-11-29 21:07:00″;
  3. Echo "The Time for Zero1 is:". $zero 1 . "
    ”;
  4. Echo "The Time for Zero2 is:". $zero 2 . "
    ”;
  5. if (strtotime($zero 1) <strtotime($zero 2 )){
  6. Echo "Zero1 earlier than Zero2″;
  7. }else{
  8. Echo "Zero2 earlier than Zero1″;
  9. }
  10. ?>

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:

 
 
    1. $zero 1 = Strtotime (date("y-m-d h:i:s")); //Current time
    2. $zero 2 = Strtotime ("2011-2-03 24:00:00″); //New Year time
    3. $guonian = Ceil (($zero 2-$zero 1)/86400); //60s*60min*24h
    4. Echo "The New Year is still $guonian Day! ”;
    5. ?>

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:

 
 
  1. function run_time ()
  2. {
  3. list ($msec, $sec) = Explode ("", Microtime ());
  4. return (float)$msec + (float)$sec);
  5. }
  6. $start _time =run_time ();
  7. $zero 1 = Strtotime (date("y-m-d h:i:s")); //Current time
  8. $zero 2 = Strtotime ("2011-2-03 24:00:00″); //New Year time
  9. $guonian = Ceil (($zero 2-$zero 1)/86400); //60s*60min*24h
  10. Echo "The New Year is still $guonian Day! ”;
  11. $end _time =run_time ();
  12. ?>

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 ...)

  • Related Article

    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.