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 Cross-time Zone Application with PHP

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

Share

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

Editor to share with you how to achieve cross-time zone application of PHP, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

1. Set the internal time zone of the program to UTC time. (UTC can also be called GMT)

PHP settings:

Date_default_timezone_set ("UTC")

Yii settings:

Add: 'timeZone'= >' UTC' to config/main.php

After this setting, the time of PHP generation is basically UTC time. For example:

/ / output the current UTC time

Date ("Y-m-d H:i:s")

two。 UTC time is stored in the database.

It can be controlled by PHP or by setting the time zone of the database.

3. The time sent by the server to the front end is in UTC time format, which is converted by JS to local time for display. JS internal data is separated from display data.

JS conversion function reference:

The copy code is as follows:

/ * *

* convert UTC time to local time

* @ param string utcTime utc time string format: 'Y-m-d Hrig ipurs'

* @ return string local time string format: 'Y-m-d halo ipurs'

, /

Function utcToLocal (utcTime) {

If (utcTime==='0000-00-0000: 00 utcTime===null | | utcTime==='' | | utcTime===undefined)

Return utcTime

Var locTime = new Date (); / / local time object

UtcTime=utcTime.replace ("-", "/") .replace ("-", "/"); / / Firefox is not compatible with'-'to separate dates

/ / parse string and local time assignment

LocTime.setTime (Date.parse (utcTime)-locTime.getTimezoneOffset () * 60000)

/ / Local time string formatting

Var year = locTime.getFullYear ()

Var month = preZero (locTime.getMonth () + 1)

Var date = preZero (locTime.getDate ())

Var hour = preZero (locTime.getHours ())

Var minute = preZero (locTime.getMinutes ())

Var second = preZero (locTime.getSeconds ())

Return year+'-'+month+'-'+date+''+ hour+':'+minute+':'+second

}

/ * *

* convert local time to UTC time

* @ param string locTime utc time string format: 'Y-m-d Hrig ipurs'

* @ return string local time string format: 'Y-m-d halo ipurs'

, /

Function localToUtc (locTime) {

If (locTime==='0000-00-0000: 00 locTime==='0000 00' | | locTime===null | | locTime==='' | | locTime===undefined)

Return locTime

Var tmpTime = new Date ()

Var utcTime = new Date ()

LocTime=locTime.replace ("-", "/") .replace ("-", "/"); / / Firefox is not compatible with'-'to separate dates

/ / parse the string

TmpTime.setTime (Date.parse (locTime))

If (locTime.length > 10) {

Var year = tmpTime.getUTCFullYear ()

Var month = preZero (tmpTime.getUTCMonth () + 1)

Var date = preZero (tmpTime.getUTCDate ())

Var hour = preZero (tmpTime.getUTCHours ())

Var minute = preZero (tmpTime.getUTCMinutes ())

Var second = preZero (tmpTime.getUTCSeconds ())

Return year+'-'+month+'-'+date +''+ hour+':'+minute+':'+second

} else {

/ / set the date and keep the local time (for UTC conversion)

UtcTime.setFullYear (tmpTime.getFullYear ())

UtcTime.setMonth (tmpTime.getMonth ()); utcTime.setMonth (tmpTime.getMonth ()); /? If it is not repeated, the assignment is invalid.

UtcTime.setDate (tmpTime.getDate ())

Var year = utcTime.getUTCFullYear ()

Var month = preZero (utcTime.getUTCMonth () + 1)

Var date = preZero (utcTime.getUTCDate ())

Return year+'-'+month+'-'+date

}

}

/ / add a leading 0 to a single number

Function preZero (str) {

Return str.toString () length

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