Home > Daily Programming > PHP Knowledge > Detailed explanation of how to get the current time and zone settings in php [Video attached]

Detailed explanation of how to get the current time and zone settings in php [Video attached]

藏色散人
Release: 2019-11-02 18:02:20
Original
5541 people have browsed it

This article mainly introduces two methods of how to obtain the current time and time zone settings using PHP, as well as methods of obtaining the current time of yesterday and tomorrow respectively.

Below we will give you a detailed explanation through specific PHP code examples.

1. Get the current Unix timestamp directly through the PHP time function

<?php
date_default_timezone_set(timezone_identifier:&#39;Asia/Shanghai&#39;);
$time=time();
echo  date(format:"Y-m-d H:i:s",$time);
Copy after login

The output result is as shown in the figure below:

Detailed explanation of how to get the current time and zone settings in php [Video attached]

( Figure 1)

In this code, we directly use the time function in PHP to obtain the current timestamp (referring to the number of seconds from January 1, 1970 to the current time). If we need to display For the normal time format in daily life, you need to use the date function to convert the timestamp format. The format of year, month, day, hour, minute and second is set here. However, even if we convert the normal time format, there may be a time difference in the current time displayed, so we must set the time zone.

One way to set the time zone in PHP is to declare it directly in the code, such as the usage statement of the date_default_timezone_set() function in the above code, in which we added the time zone of Shanghai, Asia as a parameter. So in the end, as shown in the picture above, the complete current time was obtained.

2. Get the current time through the date function

echo  date(format:"Y-m-d H:i:s");
Copy after login

In fact, the date function and the time function are used in the same way. As above, we can directly output the time obtained by date and also set the format to year. Month, day, hour, minute and second. So in order for the time to be accurate enough, we also need to set the time zone. In addition to the above methods directly declared in the code, another method is to modify the time zone in the PHP configuration file.

Open the PHP.ini configuration file as shown below, find date.timezone, delete the preceding semicolon and then set the time zone:

Detailed explanation of how to get the current time and zone settings in php [Video attached]

(Picture 2)

Then restart the PHP environment. Then the current time finally obtained is as shown in Figure 1.

3. PHP gets the time of yesterday and tomorrow

echo  date(format:"Y-m-d H:i:s",strtotime(time:&#39;-1 day&#39;));
echo  date(format:"Y-m-d H:i:s",strtotime(time:&#39;+1 day&#39;));
Copy after login

In the first line of code here, we set the "-1 day" parameter in the strtotime function, which means yesterday time. If the "1 day" parameter is set, it means tomorrow's time.

The above content is a detailed introduction to PHP's method of obtaining the current time, yesterday and tomorrow's time, and time zone settings, which has certain reference value. This article also has corresponding Video tutorial[PHP to get the current time and set time zone and common interview questions] for everyone to learn.

The above is the detailed content of Detailed explanation of how to get the current time and zone settings in php [Video attached]. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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