Home > Article > Backend Development > Function to get current time in php
When writing programs, we often need to obtain the current system time. Below we will introduce to you the function of getting the current time in PHP.
Recommended tutorial: PHP video tutorial
1. Get the current date and time string:
date("Y-m-d H:i:s"); // 2017-12-14 23:13:51
2. Get the current timestamp:
time(); // 1513264258
3. Get the date string corresponding to a certain timestamp:
date("Y-m-d H:i:s",1513264258); // 2017-12-14 23:13:51
4. Get the timestamp of a certain date
strtotime('2017-12-14 23:13:51'); // 1513264258
Sometimes you encounter a time error when obtaining the time. The obtained time is different from the actual time. The reason for this may be that the time zone setting is wrong. There are two solutions to this situation.
1. Find date.timezone in php.ini and change its value to Asia/Shanghai, that is, date.timezone = Asia/Shanghai
2. At the beginning of the program Just add date_default_timezone_set('Asia/Shanghai').
Specific usage of date(), time(), strtotime() functions
date() function
Syntax
date(format,timestamp);
Definition and usage
The date() function takes two parameters, the first is the format string, and the second (optional) is the UNIX timestamp. If no timestamp is specified, the date() function will return the current date and time by default. If yes, return the formatted date string corresponding to the timestamp.
Example:
<?php header("Content-type: text/html; charset=utf-8"); //将时区设置为上海时间 date_default_timezone_set('Asia/Shanghai'); echo date("Y-m-d H:i:s");
time() function
Syntax:
time();
Definition and usage
time() function returns the Unix epoch (January 1 1970 00:00:00 GMT ) The number of seconds (timestamp) of the current time since .
strtotime() function
Syntax:
strtotime(time,now);
Definition and Usage
The strtotime() function parses any English text date or time description into a Unix timestamp (number of seconds since January 1 1970 00:00:00 GMT).
Note: If the year representation uses a two-digit format, values 0-69 will be mapped to 2000-2069, and values 70-100 will be mapped to 1970-2000.
Note: Please note that for dates in m/d/y or d-m-y format, if the separator is a slash (/), the American m/d/y format is used. If the delimiter is a dash (-) or a dot (.), the European d-m-y format is used. To avoid potential errors, you should use YYYY-MM-DD format whenever possible or use the date_create_from_format() function.
Example:
<?php header("Content-type: text/html; charset=utf-8"); date_default_timezone_set('Asia/Shanghai'); echo(strtotime("now") . "<br>"); echo(strtotime("15 October 2019") . "<br>"); echo(strtotime("+5 hours") . "<br>"); echo(strtotime("+1 week") . "<br>"); echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>"); echo(strtotime("next Monday") . "<br>"); echo(strtotime("last Sunday")); echo(strtotime("2011-11-11 11:11:11"));
The following characters represent the format of the output date string specified by the date() function:
●d - Day of the month (from 01 to 31)
●D - Textual representation of the day of the week (in three letters)
●j - Day of the month, without leading zeros (1 to 31)
●l (lowercase 'L') - Full textual representation of the day of the week
N - ISO-8601 numeric format representation of the day of the week (1 represents Monday [Monday], 7 represents Sunday [Sunday])
● S - The English ordinal suffix of the day of the month ( 2 characters: st, nd, rd or th. Used with j)
●w - The numeric representation of the day of the week (0 means Sunday [Sunday], 6 means Saturday [Saturday])
z - day of the year (from 0 to 365)
W - week number of the year in ISO-8601 number format (week starts with Monday) )
●F - The complete text representation of the month (January [January] to December [December])
●m - The numeric representation of the month (from 01 to 12)
●M - short text representation of the month (represented by three letters)
●n - numeric representation of the month without leading zeros (1 to 12)
● t - the number of days contained in the given month
L - whether it is a leap year (1 if it is a leap year, 0 otherwise)
o - ISO-8601 The year number under the standard
Y - the four-digit representation of the year
●y - the two-digit representation of the year
●a - Lowercase form: am or pm
●A - Uppercase form: AM or PM
●B - Swatch Internet Time (000 to 999)
●g - 12 hour clock, without leading zeros (1 to 12)
G - 24 hour clock, without leading zeros (0 to 23)
h - 12 hour clock, with leading zeros (01 to 12)
H - 24-hour clock, with leading zeros (00 to 23)
i - Minutes, with leading zeros (00 to 59)
●s - seconds with leading zeros (00 to 59)
●u - microseconds (new in PHP 5.2.2)
●e - time zone identifier ( For example: UTC, GMT, Atlantic/Azores)
I (uppercase form of i) - whether the date is in daylight saving time (1 if it is daylight saving time, 0 otherwise)
●O - The difference between Greenwich Mean Time (GMT), the unit is hours (Example: 0100)
●P - The difference between Greenwich Mean Time (GMT), the unit is hours:minutes (PHP 5.1 New in .3)
●T - abbreviation for time zone (examples: EST, MDT)
●Z - time zone offset in seconds. The offset of the time zone west of UTC is a negative number (-43200 to 50400)
●c - ISO-8601 standard date (for example, 2013-05-05T16:34:42 00:00)
●r - Date in RFC 2822 format (e.g. Fri, 12 Apr 2013 12:01:05 0200)
●U - Seconds elapsed since the Unix epoch (January 1 1970 00:00:00 GMT) Number
At the same time, you can also use the following predefined constants (available since PHP 5.1.0):
●DATE_ATOM - Atom (for example :2013-04-12T15:52:01 00:00)
●DATE_COOKIE - HTTP Cookies (for example: Friday, 12-Apr-13 15:52:01 UTC)
●DATE_ISO8601 - ISO-8601 (for example: 2013-04-12T15:52:01 0000)
DATE_RFC822 - RFC 822 (for example: Fri, 12 Apr 13 15:52:01 0000)
●DATE_RFC850 - RFC 850 (for example: Friday, 12-Apr-13 15:52:01 UTC)
●DATE_RFC1036 - RFC 1036 (for example: Friday, 12 Apr 13 15:52:01 0000)
●DATE_RFC1123 - RFC 1123 (for example: Fri, 12 Apr 2013 15:52:01 0000)
●DATE_RFC2822 - RFC 2822 (Fri, 12 Apr 2013 15:52:01 0000)
●DATE_RFC3339 - Same as DATE_ATOM (as of PHP 5.1.3)
●DATE_RSS - RSS (Fri, 12 Aug 2013 15:52:01 0000)
●DATE_W3C - World Wide Web Consortium (for example: 2013-04-12T15:52:01 00:00)
The above is the detailed content of Function to get current time in php. For more information, please follow other related articles on the PHP Chinese website!