PHP learning about time_PHP tutorial

WBOY
Release: 2016-07-13 17:46:38
Original
953 people have browsed it

Functions about time:

strftime Format local time/date according to locale

strptime — parse date/time generated by strftime()

setlocale function sets regional information (regional 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, please add date_default_timezone_set('Etc/GMT-8');

date — Format a local time/date

gmdate is exactly the same as date — formats a GMT/UTC date/time

mktime — Get the Unix timestamp of a date mktime(0, 0, 0, 12, 30, 1997)

gmmktime and mktime are exactly the same

strtotime — Parse any English text datetime description into a Unix timestamp

time — Returns the current Unix timestamp

microtime — Returns the current Unix timestamp and microseconds

getlastmod — Get the time when the web content was last updated on the currently used web page

Note: TimeStamp is not a function, it refers to the Unix timestamp, which is the number of seconds from 0:00:00 on January 1, 1970 to this moment

Practical operation cases

$strtime = "2000-02-12 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: $hour
";

echo "minute: $minute
";

echo "Seconds: $second
";

echo "Convert to timestamp:" . $timestamp . "
";

echo "Convert back from timestamp:" . date("y-m-d h:i:s",$timestamp) . "
";

echo "Convert back from timestamp:" . date("y-m-d h:i:s","1288263141") . "
";

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

echo (strtotime("2010-10-28 10:52:21")). "
"; // Convert the string in MySQL format into seconds

echo (strtotime("2010-10-01 00:00:00")). "
";

$d = date("Y-m-d H:i:s", time()); // Convert seconds to timestamp

in MySQL format

echo $d. "
";

The result is:

array

0 => string '2000' (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: 2000-02-12 16:20:35

Year: 2000

Month: 02

Day: 12

Time: 16

Points: 20

Seconds: 35

Convert to timestamp: 950372435

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

Convert back from timestamp: 10-10-28 10:52:21

1288263141

1285891200

2011-10-20 14:48:27

Common values ​​for formatting time functions

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

Y: year represented by 4 digits

y: year represented by 2 digits

m: month represented by number

M: The month represented by the three-letter abbreviation

d: Day of the month

D: What day of the week

h: Hour, 12-hour format, with leading zeros

H: Hour, 24-hour format, with leading zeros

i: Minutes with leading zeros

I: Whether it is daylight saving time

s: seconds

S: English suffix after the day of the month, 2 characters

w: Day of the week, represented by numbers

W: Week number of the year in ISO-8601 format, each week starts on Monday

l: Day of the week, complete text format

L: Whether it is a leap year

g: 12 hour format, no leading zeros

G: 24 hour format, no leading zeros

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

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

Returns an associative array containing date information based on timestamp. If no timestamp is given, the current local time is assumed.

The cells in the array are as follows:

Key name

Description

Return value example

"seconds"

Numerical representation of seconds

0 to 59

"minutes"

Numerical representation of minutes

0 to 59

"hours"

Numerical representation of hours

0 to 23

"mday"

The numerical representation of the day of the month

1 to 31

"wday"

The numerical representation of the day of the week

0 (meaning Sunday) to 6 (meaning Saturday)

"mon"

Number representation of month

1 to 12

"year"

Complete year represented by 4 digits

For example: 1999 or 2003

"yday"

The numerical representation of the day of the year

0 to 365

"weekday"

Full textual representation of the day of the week

Sunday to Saturday

"month"

Full text representation of month

January to December

0

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

System dependent, typical values ​​are from -2147483648 to 2147483647.

Example:

//Display format: Year-Month-Day Hour:Minute:Miao

$showtime=date("Y-m-d H:i:s");

echo 'The format of display time is:'.$showtime."
";

The result is:

The format for displaying time is: 2011-10-20 14:48:27

This article comes from the “php technology” blog

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478577.htmlTechArticleFunctions about time: strftime formats local time/date according to regional settings strptime parses the date generated by strftime() /Time setlocale function sets regional information (regional information...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template