PHP display date and time details

Source: Internet
Author: User
Tags current time datetime iso 8601 local time locale lowercase php programming time and date

PHP's display time code is much more powerful than ASP, making it easier to call.

First Look

The code is as follows: Copy code

// Start the system function
$ Nbyear = Date ('Y ');
$ Nbmonth = Date ('M ');
$ Nbday = Date ('D ');
$ Date = Date ('Y-m-D ');
$ Datetime = Date ('Y-m-d H: I: s ');
$ Cndate = Date ('y, m, dday ');
$ Cndateweek = Date ('y, m, dday ');

Next we will introduce them one by one.

1. Year-month-day
Echo date ('Y-m-J ');
2007-02-6
Echo date ('Y-n-J ');
07-2-6
Uppercase Y indicates four digits of the year, while lowercase y indicates two digits of the year;
Lowercase m indicates the number of the month (with the leading digit), while lowercase n indicates the number of the month without the leading digit.

Echo date ('Y-M-J ');
2007-Feb-6
Echo date ('Y-m-D ');
2007-02-06
Uppercase M indicates the three abbreviations of the month, while lowercase m indicates the number of the month (with leading 0 );
There is no upper-case J. Only lower-case j indicates the date of the month, and there is no leading o. If the month needs to have the upper-case J, the lower-case d is used.
Echo date ('Y-M-J ');
2007-Feb-6
Echo date ('Y-F-js ');
2007-February-6th
Uppercase M indicates the three abbreviated characters of the month, while uppercase F indicates the full English writing of the month. (No lower case f)
Uppercase S indicates the suffix of a date, such as "st", "nd", "rd", and "th". For details, see the date number.

Summary:
Y indicates that the year can be in uppercase or lowercase y;
Indicates that the month can be written in uppercase letters (F), uppercase letters (M), lowercase letters (m), and lowercase letters (n );
Indicates the date suffix.

2. Hour: minute: Second
By default, PHP interprets the display time as "Greenwich Mean Time", which is 8 hours different from our local time.
Echo date ('G: I: s ');
5:56:57 am
Echo date ('H: I: s ');
05:56:57 AM
Lowercase g indicates the 12-hour system, with no leading 0, while lowercase h indicates the 12-hour system with leading 0.
When the 12-hour format is used, it indicates that upper afternoon, lower case a indicates lower case "am" and "pm", and upper case A indicates upper case "AM" and "PM ".

Echo date ('G: I: s ');
14:02:26
Uppercase G indicates the number of hours in the 24-hour format, but does not contain the leading value. Uppercase H indicates the number of hours in the 24-hour format.
Summary:
The letter g indicates that the hour does not contain a leading character, and the letter h indicates that the hour contains a leading character;
Lowercase g and h are in 12-hour format, while uppercase G and H are in 24-hour format.

3. Leap year, week, and day
Echo date ('L ');
This year's leap year: 0

Echo date ('L ');
Today is: Tuesday
Echo date ('D ');
Today is: Tue
Capital L indicates whether to determine the leap year of the year. If it is true, 1 is returned; otherwise, 0 is returned;
Lowercase l indicates the day of the week in full (Tuesday );
However, uppercase D is used to represent the three abbreviation of the day of the week (Tue ).

Echo date ('w ');
Today's week: 2
Echo date ('w ');
This week is the 06th week of the year.
Lowercase w indicates the day of the week, expressed in numbers
In upper case, W indicates the number of weeks in a year.

Echo date ('t ');
This month is 28 days
Echo date ('Z ');
Today is the 36th day of this year
Lowercase t indicates the number of days in the current month
Lowercase z indicates that today is the day of the year

4. Others
Echo date ('t ');
UTC
Uppercase T indicates the time zone setting of the server

Echo date ('I ');
0
If I is used to determine whether the current value is success, 1 is returned for true. Otherwise, 0 is returned.

Echo date ('u ');
1170769424
The upper-case u table shows the total number of seconds from January 1, January 1, 1970 to the present, which is the Unix timestamp of the UNIX time era.

Echo date ('C ');
2007-02-06T14: 24: 43 + 00: 00
Lowercase c represents the ISO8601 date, the date format is YYYY-MM-DD, with the letter T to interval the date and time, the time format is HH: MM: SS, the time zone uses Greenwich Mean Time (GMT).

Echo date ('r ');
Tue, 06 Feb 2007 14:25:52 + 0000
Lowercase r indicates RFC822 date

The parameters are as follows:
A-"am" or "pm"
A-"AM" or "PM"
D-a few days, two digits. If there are less than two digits, fill in the first zero. For example, "01" to "31"
D-the day of the week, with three English letters, for example, "Fri"
F-month, full English name; for example: "January"
H-12 hours, for example, "01" to "12"
H-24 hours, for example, "00" to "23"
Hours in the g-12 hour format. Less than two hours do not fill in zero. For example: "1" to 12"
Hours in the G-24-hour format. Less than two hours do not fill in zero. For example: "0" to "23"
I-minutes; for example: "00" to "59"
J-a few days, two digits. If less than two digits are left blank, for example, "1" to "31"
L-the day of the week, full name in English; for example: "Friday"
M-month, two digits. If there are less than two digits, add zero in front, for example, "01" to "12"
N-month, two digits. If less than two digits are left blank, for example, "1" to "12"
M-month, with three English letters, for example, "Jan"
S-seconds; for example: "00" to "59"
The end of the S-character is followed by an English sequence, with two English letters, for example, "th", "nd"
T-specifies the number of days in a month, for example, "28" to "31"
U-total seconds
W-number of weeks, for example, "0" (Sunday) to "6" (Saturday)
Y-year, four digits, such as: "1999"
Y-year, two digits, for example, "99"
Z-the day of the year, for example, "0" to "365"
If the displayed time is different from the system time, modify the PHP. ini configuration file.
The default UTC time is used.
You can open php. inc.
Set date. timezone = PRC

PHP displays the localized date and time


1. setlocale () function
The setlocale () function can change the default PHP localization environment.
Syntax format: setlocale (category locale)
If the locale parameter is null, the locale or lang value of the system environment variable will be used. Otherwise, the localized environment specified by the locale parameter will be applied. For example, en_US is an American localization environment, chs is simplified Chinese, and cht is traditional Chinese.
The category parameter options are described as follows:
LC_ALL & mdash;-contains all the following localization rules.
LC_COLLATE -- string comparison
LC_CTYPE -- string classification and conversion, such as case-insensitive conversion
LC_MONETARY-currency format of the localization environment
LC_NUMERIC -- numerical form of the localization environment
LC_TIME -- time format of the local environment

2. strftime () function
Strftime () function-format the output date and time based on the localization environment.
Syntax format: strftime (format timestamp)
This function returns the string output after the timestamp parameter is formatted with the given string. If no timestamp is provided, the local time is used.
Conversion tag for format parameter recognition:
% A-abbreviation of the day of the week in the current region
% A-full name of the current region Day of the week
% B-abbreviation of the month in the current region
% B-full name of the current region month
% C-the preferred datetime expression for the current region
% C-century value (year divided by 100 and then rounded up from 00 to 99)
% D-the day of the month, in decimal number (range: 01 to 31)
% D-the same as % m/% d/% y
% E-the day of the month, in decimal number. A space (ranging from '1 & prime; to '31 & prime;) is added before a digit ;)
% G-same as % G, but not Century
% G-4-digit year, which conforms to the ISO week count (see % V ). The format and value of % V are the same, except that the number of ISO weeks belongs to the previous year or the next year, the year is used.
% H-the same as % B
% H-24 hours in decimal format (range: 00 to 23)
% I-12 hours in decimal format (range: 00 to 12)
% J-the day of the year in decimal format (ranging from 001 to 366)
% M-decimal month (range: 01 to 12)
% M-decimal minutes
% N-linefeed
% P-the value is 'am' or 'PM 'based on the given time, or the corresponding string in the current region settings
% R-time with the.m. and p.m. symbols
% R-24 hour time
% S-decimal seconds
% T-tab
% T-current time, same as % H: % M: % S
% U-the day of the week in decimal format [1, 7]. 1 indicates Monday.
Warning
Although ISO 9889: 1999 (the current C standard) clearly states that a week starts from Monday, Sun Solaris's week seems to start from Sunday and act as 1.
% U-the week of the current year, starting from the first Sunday of the first week as the first day
% V-ISO 8601: 1988 format for the week of the current year, ranging from 01 to 53. Week 1st is the first week of the current year with at least four days, and Monday is the first day of each week. (% G or % g is used as the year of the corresponding week of the specified timestamp .)
% W-the week number of the current year, starting from the first Monday of the first week
% W-the day of the week, and Sunday is 0
% X-the preferred time representation for the current region, excluding the time
% X-the preferred time representation for the current region, excluding the date
% Y-there is no century in decimal year (range: 00 to 99)
% Y-the year in decimal format of century
% Z or % z-time zone name or abbreviation
%-Characters '%' in text

Example: output the localization time and date in different ways

Code:

The code is as follows: Copy code
<? Php
Setlocale (LC_ALL, "en_US ");
Echo "US format:". strftime ("Today is % ");
Echo "<p> ";
Setlocale (LC_ALL, "chs ");
Echo "China format:". strftime ("Today is % ");
Echo "<p> ";
Echo "simplified Chinese month:". strftime ("this month is % B ");
?>

Output result:

US format: Today is Thursday

China format: Today is Thursday

Month in simplified Chinese: This month is November

In the PHP programming process, we have to consider different time expressions, and output time and date in different ways according to different regions. The above describes how to use the setlocale () function and strftime () function to set the localization environment and format the output time and date.

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.