Php time learning

Source: Internet
Author: User
Time Functions: strftime format local time/date strptime & mdash according to region settings; parse the date/time setlocale function generated by strftime () to set region information (Region information) date_default_timezone_set if the result of the date function is found to be small... syntaxH

 

Time functions:

 

Strftime format local time/date according to region settings

 

Strptime-parse the date/time generated by strftime ()

 

Setlocale function sets region information (Region information)

 

Date_default_timezone_set if you find that the number of hours obtained by the date function is 8 hours different from the actual number of hours, add date_default_timezone_set ('etc/GMT-8 ');

 

Date-format a local time/date

 

Gmdate is exactly the same as date-format a GMT/UTC date/time

 

Mktime-obtain a date's Unix timestamp mktime (0, 0, 0, 12, 30,199 7)

 

Gmmktime and mktime are identical.

 

Strtotime-parses the date and time descriptions of any English text into Unix timestamps

 

Time-returns the current Unix timestamp

 

Microtime-returns the current Unix timestamp and number of microseconds

 

Getlastmod-get the last time the webpage content is updated in use

 

 

 

Note: TimeStamp is not a function that refers to the Unix TimeStamp, that is, the number of seconds from January 01, 1970 00:00:00 to the moment.

 

 

 

Case studies

 

 

 

 

 

$ Strtime = "16:20:35 ";

 

 

 

$ Array = explode ("-", $ strtime );

 

$ Year = $ array [0];

 

$ Month = $ array [1];

 

Var_dump ($ array );

 

$ Array = explode (":", $ array [2]);

 

$ Minute = $ array [1];

 

$ Second = $ array [2];

 

Var_dump ($ array );

 

 

 

$ Array = explode ("", $ array [0]);

 

$ Day = $ array [0];

 

$ Hour = $ array [1];

 

 

 

$ Timestamp = mktime ($ hour, $ minute, $ second, $ month, $ day, $ year );

 

 

 

Echo "String Time: $ strtime
";

 

Echo "year: $ year
";

 

Echo "month: $ month
";

 

Echo "day: $ day
";

 

Echo ": $ hour
";

 

Echo ": $ minute
";

 

Echo "Seconds: $ second
";

 

Echo "converted to timestamp:". $ timestamp ."
";

 

Echo "converted back from timestamp:". date ("y-m-d h: I: s", $ timestamp )."
";

 

Echo "converted back from timestamp:". date ("y-m-d h: I: s", "1288263141 ")."
";

 

 

 

 

 

Header ("Content-Type: text/html; charset = utf-8 ");

 

Echo (strtotime ("10:52:21 "))."
"; // MySQL format string converted to seconds

 

Echo (strtotime ("00:00:00 "))."
";

 

$ D = date ("Y-m-d H: I: s", time (); // converts seconds to timestamp in MySQL format

 

Echo $ d ."
";

 

 

 

Result:

 

 

 

Array

 

0 => string '20140901' (length = 4)

 

1 => string '02' (length = 2)

 

2 => string '12 16:20:35 '(length = 11)

 

Array

 

0 => string '12 16' (length = 5)

 

1 => string '20' (length = 2)

 

2 => string '35' (length = 2)

 

String Time: 16:20:35

Year: 2000

Month: 02

Day: 12

Hour: 16

Score: 20

Seconds: 35

To timestamp: 950372435

Converted back from timestamp: 00-02-12 04:20:35

Return from timestamp: 10-10-28 10:52:21

1288263141

1285891200

2011-10-20 14:48:27

 

 

 

 

 

Common formatting time functions

 

 

 

The date (string format [, int timestamp]) function can format the date/time. the format parameter is a formatted string. The most common values are as follows:

 

Y: The year in which the four digits represent the complete number.

Y: The Year in two digits.

M: The month represented by a number.

M: The month abbreviated to three letters.

D: The day of the month

 

D: The day of the week

H: hour, 12-hour format, with leading zero

H: hour, 24-hour format, with leading zero

I: The number of minutes with leading zeros

 

I: indicates whether it is enabled.

 

S: Seconds

 

S: English suffix after the number of days per month, 2 characters

 

W: The day of the week, which is a number.

 

W: Week in ISO-8601 format year, starting from Monday

 

L: Day of the week, in the complete text format

 

L: whether it is a leap year

 

G: 12-hour format, no leading zero

 

G: 24-hour format, no leading zero

 

 

 

If you find that the hour produced by the date function is 8 hours different from the actual, add date_default_timezone_set ('etc/GMT-8 ');

 

The getdate (timestamp) function can obtain date/time information.

 

Returns a combined array containing date information based on timestamp. If no timestamp is provided, it is considered as the current local time.

 

The elements in the array are as follows:

 

Key name

Description

Return value example

 

"Seconds"

Number of seconds

0 to 59

 

"Minutes"

Minute numeric representation

0 to 59

 

"Hours"

Number of hours

0 to 23

 

"Mday"

Number of the day of the month

1 to 31

 

"Wday"

Number of the day of the week

0 (Sunday) to 6 (Saturday)

 

"Mon"

Number of months

1 to 12

 

"Year"

The full year represented by four digits

Example: 1999 or 2003

 

"Yday"

Number of the day of the year

0 to 365

 

"Weekday"

Complete text representation of the day of the week

Sunday to Saturday

 

"Month"

Complete text representation of the month

January to December

 

0

The number of seconds since the beginning of the Unix era, similar to the return value of time () and the value used for date.

System related. the typical value is from-2147483648 to 2147483647.

 

 

 

Instance:

 

// Display format: year-month-day hour: minute: wonderful

 

$ Showtime = date ("Y-m-d H: I: s ");

 

Echo 'display time format: '. $ showtime ."
";

 

Result:

 

Display time format: 14:48:27

 

 

 

This article is from the "php technology" blog

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.