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 realize timestamp conversion by php

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces php how to achieve timestamp format, the article is very detailed, has a certain reference value, interested friends must read it!

Php to achieve timestamp conversion methods: 1, through the strtotime to obtain the unix timestamp of the specified date; 2, the use of date function to convert the specified timestamp into the system time format.

This article operating environment: Windows7 system, PHP7.1 version, Dell G3 computer.

Conversion of timestamp and date format in php

Echo strtotime ("2009-1-22") result: 1232553600

Description: return to 00:00:00 timestamp on January 22nd, 2009

Second, an example of the date and time when the PHP timestamp function obtains the English text is as follows:

For easy comparison, use date to convert the current timestamp and the specified timestamp into system time

(1) print the timestamp strtotime ("+ 1 day") at this time tomorrow

Current time: echo date ("Y-m-d H:i:s", time ()) result: 2009-01-22 09:40:25

Specified time: echo date ("Y-m-d H:i:s", strtotime ("+ 1 day")) result: 2009-01-23 09:40:25

(2) print the timestamp strtotime ("- 1 day") of this time yesterday.

Current time: echo date ("Y-m-d H:i:s", time ()) result: 2009-01-22 09:40:25

Specified time: echo date ("Y-m-d H:i:s", strtotime ("- 1 day")) result: 2009-01-21 09:40:25

(3) print the timestamp strtotime ("+ 1 week") at this time next week.

Current time: echo date ("Y-m-d H:i:s", time ()) result: 2009-01-22 09:40:25

(4) print the timestamp strtotime ("- 1 week") at this time last week.

Current time: echo date ("Y-m-d H:i:s", time ()) result: 2009-01-22 09:40:25

(5) print the timestamp strtotime ("next Thursday") that specifies the day of next week.

Current time: echo date ("Y-m-d H:i:s", time ()) result: 2009-01-22 09:40:25

Specified time: echo date ("Y-m-d H:i:s", strtotime ("next Thursday")) result: 2009-01-29 00:00:00

(6) print the timestamp strtotime ("last Thursday") that specifies the day of the previous week.

Current time: echo date ("Y-m-d H:i:s", time ()) result: 2009-01-22 09:40:25

As shown in the above PHP timestamp function example, strtotime can parse the date-time description of any English text into a Unix timestamp. We use mktime () or date () to format the date-time to obtain the specified timestamp to achieve the required date-time.

Example:

The parts that need to be paid attention to

There are two types of PHP time, one is the timestamp type (1228348800), the other is the normal date format (2008-12-4).

The time stamp above php5.1 will differ from the actual time by 8 hours. The solution is as follows

1. The easiest way is not to use a version above php5.1-obviously this is not a desirable method!

2. Modify php.ini. Open php.ini to find date.timezone, remove the semicolon after the semicolon = add Asia/Shanghai, and restart the apache server-- the disadvantage is that if the program is placed on someone else's server and cannot modify the php.ini, there is nothing you can do.

3, add the initialization statement of time in the program, that is, "date_default_timezone_set (" Asia/Shanghai ");" this can be set by the programmer at will, my recommendation.

Time zone identifier, the values available on the mainland are: PRC,Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (followed by China, Chongqing, Shanghai, Urumqi), Etc/GMT-8,Asia/Harbin

Available in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (Macau, Hong Kong, Taipei in turn)

And Singapore: Asia/Singapore

In this way, the output is Beijing time.

Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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