Home >Backend Development >PHP Problem >How to get the current time in php

How to get the current time in php

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-30 10:00:0715464browse

How to get the current time in php

1. Get the current timestamp

Method 1: Through the time function

time();

Method 2: Through the REQUEST_TIME element in $_SERVER

$_SERVER['REQUEST_TIME'];

Method 3: Through the strtotime function

strtotime('now');

2. Get the current time

Through the date function format Timestamp

echo date('Y-m-d h:i:s', time()); // 2018-10-3 15:57:05

3. Time zone problem

The above methods all have time zone problems, specific solutions:

Method 1: Modify in php.ini Set the Chinese time zone

date.timezone = PRC

Method 2: Temporarily set the Chinese time zone in the php file

date_default_timezone_set('PRC');

Related recommendations: "PHP Tutorial"

php gets the current time and timestamp

The first thing you need to know is that the method to get the time in php is date(), and the methods to get the timestamp in php are time() and strtotime(). The following are explained respectively:

date() format is: date($format, $timestamp), format is the format, timestamp is the timestamp (optional).

time() returns the Unix timestamp of the current time, without arguments.

strtotime($time, $now) Parses any English text datetime description into a Unix timestamp. $time is required and specifies the time string to be parsed; $now is used to calculate the timestamp of the return value. If this parameter is omitted, the current time is used.

date($format) usage examples:

echo date('Y-m-d');输出结果:2018-10-03
echo  date('Y-m-d H:i:s');输出结果:2018-10-03 23:00:00
echo  date('Y-m-d', time());输出结果:2018-10-03 23:00:00(结果同上,只是多了一个时间戳参数)
(时间戳转换为日期格式的方法)
echo  date('Y').'年'.date('m').'月'.date('d').'日',输出结果:2018年10月3日

These are just a few examples, just format changes. The following is the meaning of each letter in the string format:

a - "am" or "pm"

A - "AM" or "PM"

d - Day, two digits, if less than two If the digits are digits, add zeros in front; such as: "01" to "31"

D - day of the week, three English letters; such as: "Fri"

F - month, full English name; Such as: "January"

h - hour in 12-hour format; such as: "01" to "12"

H - hour in 24-hour format; such as: "00" to "23 "

g - Hours in 12-hour format, do not add zeros if there are less than two digits; such as: "1" to 12"

G - Hours in 24-hour format, do not add zeros if there are less than two digits ; For example: "0" to "23"

i - minutes; For example: "00" to "59"

j - days, two digits, if there are less than two digits, no additional digits will be added Zero; such as: "1" to "31"

l - day of the week, full English name; such as: "Friday"

m - month, two digits, if less than two digits Add zeros in front; such as: "01" to "12"

n - month, two digits, if there are less than two digits, no zeros are added; such as: "1" to "12"

M - month, three English letters; such as: "Jan"

s - seconds; such as: "00" to "59"

S ​​- add an English ordinal number at the end of the word, two English letters; such as: "th", "nd"

t - the number of days in the specified month; such as: "28" to "31"

U - the total number of seconds

w - Numeric day of the week, such as: "0" (Sunday) to "6" (Saturday)

Y - Year, four digits; such as: "1999"

y - year, two digits; such as: "99"

z - day of the year; such as: "0" to "365"

time() Usage examples:

time();输出结果:1332427715(返回的结果即当前的时间戳)
strtotime($time)用法举例:
echo strtotime('2012-03-22');输出结果:1332427715(此处结果为随便写的,仅作说明使用)
echo strtotime(date('Y-d-m'));输出结果:(结合date(),结果同上)(时间日期转换为时间戳)
strtotime()还有个很强大的用法,参数可加入对于数字的操作、年月日周英文字符,示例如下:
echo date('Y-m-d H:i:s',strtotime('+1 day'));输出结果:2012-03-23 23:30:33(会发现输出明天此时的时间)
echo date('Y-m-d H:i:s',strtotime('-1 day'));输出结果:2012-03-21 23:30:33(昨天此时的时间)
echo date('Y-m-d H:i:s',strtotime('+1 week'));输出结果:2012-03-29 23:30:33(下个星期此时的时间)
echo date('Y-m-d H:i:s',strtotime('next Thursday'));输出结果:2012-03-29 00:00:00(下个星期四此时的时间)
echo date('Y-m-d H:i:s',strtotime('last Thursday'));输出结果:2012-03-15 00:00:00(上个星期四此时的时间)

That’s all the above examples, you can study more by yourself. The strtotime() method can control the display of Unix timestamps through English text to get the required time. Date format.

php Gets the number of milliseconds in the current time

php itself does not provide a function that returns the number of milliseconds, but it provides the microtime() method, which will return a The array contains two elements: one is the number of seconds and the other is the number of milliseconds expressed as a decimal. We can obtain the returned number of milliseconds through this method. The method is as follows:

function getMillisecond(){
    list($s1,$s2)=explode(' ',microtime());
    return (float)sprintf('%.0f',(floatval($s1)+floatval($s2))*1000);

The current time and the actual time taken are Solution to the time difference of 8 hours

In actual development, it is often encountered that the obtained time is 8 hours different from the actual time of the current system. This is because of the time zone setting problem. For this problem, there are the following Several solutions:

1. Find date.timezone in php.ini and change its value to Asia/Shanghai, that is, date.timezone = Asia/Shanghai (set the current time zone to Asia Shanghai time zone )

2. Add date_default_timezone_set('Asia/Shanghai'); at the beginning of the program.

The above is the detailed content of How to get the current time in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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

Related articles

See more