Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the time zone in PHP

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to set the time zone in PHP, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

1. What is a timestamp?

How do you represent time in reality?

China: 03:30 on October 9, 2020

Foreign countries: 9, 10, 2020

The expression of time is different in countries all over the world, which makes it inconvenient for computer storage and calculation.

Therefore, the unified way of using time in the computer is to use the unix timestamp.

A) timestamp: the number of seconds from 00: 00 minutes on January 1, 1970 to the specified time. The timestamp is an integer data type.

B) the php timestamp starts at 1970, and the maximum is up to around January 27th, 2038. Because this timestamp is out of range of integers.

two。 How to set the time zone

A) obtain the current timestamp through the time function

The timestamp cannot be understood by the naked eye, so it is necessary to format the timestamp and convert it into a time representation that people can understand. Because the time of the conversion is different in the time zone, it is necessary to set the time zone first.

/ / get the current timestamp $time = time (); var_dump ($time); / / format the timestamp and convert it to $date = date that people can understand; echo $date.''

B) how to set the time zone: the default time of the server is Greenwich, so you need to set the time zone of the server before using the server.

C) setting time zone method

In both ways, set date.timezone = Asia/Shanghai or = PRC in php.ini (the time zone list is in the php manual), and then the server restart takes effect. Another way is to use the date_default_timezone_set () function in the php script to modify the time zone of the current page.

/ / set time zone (either way) date_default_timezone_set ('PRC'); date_default_timezone_set (' Asia/Shanghai'); 3. Time-related function

Time () gets the current timestamp

Parameter 1: none

Return value: returns the current timestamp

Date () formats a timestamp

Parameter 1: the string to convert the time format

Parameter 2: optional parameter. If you specify a timestamp to format the timestamp, if no timestamp is specified, the current time is defaulted.

Mktime () gets the timestamp of a date (given a date, returns the timestamp)

Return value: timestamp of the specified date

Parameters from left to right: hours, minutes, seconds, month, day, year

Parameters can be omitted from right to left.

Php common time functions are sorted out:

(1. Timestamp: total seconds from 1970-1-1: 00 (the first year of the computer) to this time / / time () / return timestamp

(2) with subtle timestamp: microtime (true); / / return timestamp / / false is more accurate, but it is more troublesome to deal with.

(3. Set time zone: date_default_timezone_set ("PRC"); / / Asia/Shanghai Asia/Hong_Kong

(4) timestamp format string: date ("Y-m-d H:i:s", time ()); / / returns the formatted time string

Other commonly used format characters: the day of the week [w], the day of the year [z], the total number of days of the month [t],

Whether it is a leap year [L], lowercase afternoon (am,pm) [a] uppercase [A], leading zero 12-hour format [h]

(5) get the timestamp 1:mktime of a specific time ($hour, $minute, $second, $month, $day, $year); / / return the timestamp

(6. Get the timestamp 2:strtotime for a specific time ("2018-11-11 16:10:56"); / / return timestamp for success and false for failure

Thank you for reading this article carefully. I hope the article "how to set the time zone in PHP" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report