现在有一个跨时区的应用,不同时区登录的用户需要看到自己时区的时间,同时也要能够进行时区的切换。
我的思路是,系统中所有存储的时间都是GMT(UTC)时间,用户登录时,根据用户所在的时区进行对应的显示。
首先了解一下PHP中时区的设置方法。PHP中进行设置的方法比较灵活多样,可以在php.ini中设置date.timezone属性、可以通过代码,调用ini_set(‘date.timezone’, ‘’)设置,也可以使用函数 date_default_timezone_set(),或者在htaccess文件中设置。
服务器的默认时区,如果设置的和我们希望的时区不符,而且我们也没有权限修改全局的时区配置,就只有借助于代码了。
PHP还提供了一个方便的函数,gmdate(),可以让我们不用关心服务器的时区设置而始终获得GMT时间,我的思路就是基于这个函数。
我的项目中使用了Codeigniter这个框架,框架中的date这个helper提供了几个方便的函数,可以用来处理应用中的多时区情况。
其中 now() 始终返回的是gmt的当前时间;local_to_gmt() 可以将本地的时间转换为gmt时间;gmt_to_local() 可以将gmt时间转换为本地时间;
考虑一个典型的应用场景:
用户登陆后,要显示当前时间。这是我们可以使用now()获得标准的gmt时间,然后使用gmt_to_local()函数转化为用户所在时区的时间。
用户要发布一个时间。用户发布了一个“2010-07-10 18:30:00”的时间,我们不能直接存入数据库,必须先利用local_to_gmt() 转化标准的gmt时间存入数据库,这样才能保证整个系统中的时间保持一致。
这两个函数的细节,其实都是根据时区,然后进行相应的运算得来。计算的时候,也可以考虑夏令时,但是所在时区夏令时的开始和结束时间,则需要自己维护。
codeigniter中提供了一份较为完整的时区列表,timezone_menu() 可以显示一个时区的下拉列表,但是这个列表中的时间不能完全对应到PHP自带的时区显示上,这是PHP本身的问题,不过可以通过下面这个函数,来让输入的每个时区,都可以获得一个对应的时区文字显示。
if( ! function_exists("tz_offset_to_name") ) { /* Takes a GMT offset (in hours) and returns a timezone name */ function tz_offset_to_name($offset) { $offset *= 3600; // convert hour offset to seconds $abbrarray = timezone_abbreviations_list(); foreach ($abbrarray as $abbr) { foreach ($abbr as $city) { if ($city['offset'] == $offset) { return $city['timezone_id']; } } } return FALSE; } }

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.