Front-end PHP entry -020-key Date function acquisition period time information function

Source: Internet
Author: User
Tags echo date integer numbers time zones local time

You need to know a few concepts about time: timezone/World time/unix timestamp

1. Time zone
This concept has been heard a lot before. We have to say a few words, we use real life in the real area, in the computer is also the same rules.
When the international Longitude Conference was held in Washington in 1884, in order to overcome the chaos of time, the world was divided into 24 time zones.
In China, the capital Beijing is located in the East eight district time for the national uniform use time.

2. The GMT
Not only are astronomers using Greenwich Mean Time (abbreviated as GMT), it is also often seen in newspapers and periodicals. We know that there are local times everywhere. If there is a major international event, the local time to record, you will feel complex inconvenience. And it's easy to make mistakes in the days ahead. As a result, astronomers have come up with an acceptable and convenient way to keep track of the local time in Greenwich, a region of the UK.

3.unix time Stamp
The computer itself does not know the time, we set a time in the computer convenient operation. So we have a way of calculating Unix timestamps.
The number of seconds elapsed from the Unix era (January 1, 1970 0 o'clock) to a time.

1. Set the time zone
If we are a multinational multi-language party program, we usually write a time zone in the configuration file, each time the program runs. Will read the settings in this time zone to show the time.

The functions for setting the time zone are:
1). Date_default_timezone_get ()
2). Date_default_timezone_set ()

The first function we do not focus on the explanation, relatively simple.

Use the following:

String Date_default_timezone_get (void)

Function:
Gets the default time zone used by all DateTime functions in a script

 
   
  
  1. <?php
  2. echo date_default_timezone_get ();
  3. ?>

The second function is the focus:
Use the following:

BOOL Date_default_timezone_set (string $timezone _identifier)

Function: The default time zone for all datetime functions

Example:

  
 
  1. <?php
  2. //定义一下时区常量,以后你可以放到配置文件里
  3. define(‘TIME_ZONE‘,‘Asia/shanghai‘);
  4. //执行函数
  5. date_default_timezone_set(TIME_ZONE);
  6. echo date(‘Y-m-d H:i:s‘);
  7. ?>

Note: Please see the official manual for the time zone list http://php.net/manual/zh/timezones.php

2.time () Gets the current UNIX timestamp

The function of the time () function is to get the Unix timestamp of the current time.
The following code outputs the UNIX timestamp for the current time.

 
   
  
  1. <?php
  2. $time=time();
  3. print_r( $time);
  4. ?>

3. "YMD" is the key to PHP learning time processing

    • Y is year in English and represents years for year
    • M represents month in English, for the months
    • D English Representative day, for the date represented

So we need to output the previous year, month, and date words.
For example: July 1, 1997, we can use the above three parameters.

 
   
  
  1. <?php
  2. echo date(‘Y年m月d日‘);
  3. ?>

You can run the code and try it out and see if it shows up.

There are a few more parameters later:

    • H:m:sIt stands for: time and minute
    • h in English: Hour on behalf of the hour
    • I in English: minute stands for minutes
    • S in English: second stands for seconds
 
   
  
  1. <?php
  2. //就可以显示出来当前的时间了哟。
  3. echo date(‘Y-m-d H:i:s‘);
  4. ?>

date函数Used to format the output of a time for easy display or storage of time. The syntax format is as follows:

String Date (string F oRRNaT[,INT f o r r n a t [ , i n t Tirnestamp])

In the parameter list:

character Description return value
D The day ordinal of a month with a leading zero 2-digit number 01 to 31
D English day of the week, 3 letters Mon to Sun
J The day ordinal of the month without leading zeros 1 to 31
L (Letter) English Day of the week Sunday to Saturday
N 1 format number of the week represented 1 (for Monday) to 7 (= Sunday)
S English suffix, 2 characters, after the number of days per month st,nd,rd or th. Can be used with the JG.
W Day of the week, numbers indicate 0 (for Sunday) to 6 (= Saturday)
Z The day ordinal of a year 0 to 366
W Week of the year, starting from Monday 42 (42nd Week of the year)
F month, full text format January to December
M The number represents the month with a leading zero 01 to 12
M 3 letter abbreviation for month Jan to Dec
N Number indicates month, no leading zero 1 to 12
T The number of days that a given month should be 28 to 31
L Whether it is a leap year If the leap year is 1, otherwise o
O Format Year number For example 2007
Y 4-digit full year indication For example, 1999 or 2008
Y Year 2-digit representation For example, 99 or 08
A Lowercase morning and afternoon values AM or PM
A Uppercase morning and afternoon values AM or PM
G Hours, 12-hour format, no leading zeros 1 to 12
G Hours, 24-hour format, no leading zeros 0 to 23
I Number of minutes with leading zeros 00 to 59
S Number of seconds with leading zeros 00 to 59
E Time zone identification
U Number of seconds since the beginning of the UNIX era Long-integer Numbers

3.getdate Get current system time

getdateUsed to get the current system time, or to get a timestamp of the specific meaning. The timestamp is a long integer that represents the syntax format of the getdate as shown below.

Array getdate ([int $timestamp = time ()])

The return value of a function is an array of time information obtained from timestamp. If there are no parameters, the current time is returned. GETDATE returns an array of key names that include full information about the time and date.

Key Name description return value
secnods seconds digits 0 to 0
minutes minutes number from + to +
hours hours number 0 to +
mday month of day number 1 to +
wday Day of the week number 0 (for Sunday) to 6 (for Saturday)
Mon Month number 1 through
Year years 4-digit full year
yday Day of the year number 0 to 365
Weekday weeks of English Sunday to Saturday
month English January to December
0 number of seconds since the Unix era long integer number

The following code can return the details of the getdate array

 
   
  
  1. <?php
  2. $mytime= getdate();
  3. print_r( $mytime);
  4. ?>

Print_r can output all the key names and values in an array.
The program outputs the time and date details of the current computer:

  
 
  1. Array
  2. (
  3. [seconds] => 1 //秒
  4. [minutes] => 10 //分钟
  5. [hours] => 17 //小时
  6. [mday] => 18 //日
  7. [wday] => 0 //星期中的第几天
  8. [mon] => 1 //月
  9. [year] => 2015 //年
  10. [yday] => 17 //年中的第几天
  11. [weekday] => Sunday //星期
  12. [month] => January //月份
  13. [0] => 1421597401 //时间戳
  14. )

After understanding the GETDATE function and the returned array, it is easy to get the current time information.
The GETDATE function obtains time information and calls the value of the return time array to output time information.

  
 
  1. <?php
  2. $mytime = getdate();
  3. echo "年 :".$mytime[‘year‘]."\n";
  4. echo "月 :".$mytime[‘mon‘]."\n";
  5. echo "日 :".$mytime[‘mday‘]."\n";
  6. echo "时 :".$mytime[‘hours‘]."\n";
  7. echo "分 :".$mytime[‘minutes‘]."\n";
  8. echo "秒 :".$mytime[‘seconds‘]."\n";
  9. echo "一个小时中的第几钟 :".$mytime[‘minutes‘]."\n";
  10. echo "这是一分钟的第几秒 :".$mytime[‘seconds‘]."\n";
  11. echo "星期名称 :".$mytime[‘weekday‘]."\n";
  12. echo "月份名称 :".$mytime[‘month‘]."\n";
  13. echo "时间戳 :".$mytime[0]."\n";
  14. ?>


From for notes (Wiz)

Front-end PHP entry -020-key Date function acquisition period time information function

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.