Inaccurate data () functions and time in php

Source: Internet
Author: User
Tags echo date ini local time lowercase php tutorial ranges

Date_default_timezone_set (PRC );

/* Set the time to Beijing time. The default php Tutorial 5 is Greenwich mean time */

Date ()

A: "am" or "pm"

A: "AM" or "PM"

D: Day (s), double-digit number. If it is not enough, it will be zero. From "01" to "31"

D: The day of the week. Three English letters, for example, "Fri"

F: Month, full English name, for example, "January"

H: Hour in 12-hour format, from "01" to "12"

H: Hour in 24-hour format, from "00" to "23"

G: the hour in 12-hour format. The value ranges from "1" to "12"

G: the hour in the 24-hour format. The value ranges from 0 to 23"

J: A few days. The number of days from "1" to "31" is not zero"

L: The day of the week. Full English name, for example, "Friday"

M: month, double digit, from "01" to "12"

N: month, double digit, not zero; from "1" to "12"

M: Month, 3 English letters; for example: "Jan"

S: seconds; from "00" to "59"

S: the end of the word is followed by an English ordinal number. There are two English letters, for example, "21th"

T: specifies the number of days in a month, from "28" to "31"

U: total number of seconds

W: number of weeks, from "0 (Sunday)" to "6 (Saturday )"

Y: year, four digits

Y: year, double digit

Z: The day of the year; from "1" to "366"

========================================================== ======================================

1, year-month-day

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.

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.

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, 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 the RFC822 date.

5. Format the time

Echo $ row ["t_time"]; returns 12:08:00

Echo date ("Y-m-d", strtotime ($ row ["t_time"]); returns

Note: Because the time obtained by $ row ["t_time"] is already a string, you need to use strtotime (string to timestamp) to convert it. Otherwise, the error-01-01 will be output.


When echo $ showtime = date ("Y-m-d H: I: s") is used to obtain the system time today, it is found that the obtained time is inconsistent with the system time.

After some research, we found that the default time set by PHP is based on Greenwich Mean Time zone, so we must change the time zone of PHP to Beijing time.

Operation method:

Open the php. Ini file, usually in the PHP installation root directory
Find the; date. timezone
Delete The Semicolon before date. timezone and change it to date. timezone = PRC.
Save the disk and restart the Apahce service (sometimes there is a problem with the Apache restart function. We recommend that you stop and start again)
Let's repeat echo $ showtime = date ("Y-m-d H: I: s ").


<? Php
Echo date ('Y-m-D ');

?>
The solution is to modify the date. timezone in the php. Ini file under the php installation directory.
Modify

[Date]
; Defines the default timezone used by the date functions
Http://php.net/date.timezone
; Date. timezone =

Is

[Date]
; Defines the default timezone used by the date functions
Http://php.net/date.timezone
Date. timezone = Asia/Shanghai

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.