设置php中的时间偏移量 [英] Set time offset in php

查看:446
本文介绍了设置php中的时间偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法返回由date()函数返回的所有日期以抵消一定的时间?
我尝试了date-default-timezone-set(),似乎没有在服务器上工作,还有其他的方法吗?

解决方案

您还可以编写自己的date()函数,该函数返回您需要的回调处理的日期(添加偏移量)。

 <?php 

函数date_offset($ format,$ offset)
{
$ time = time()+ $ offset ; #$ offset应该以毫秒为单位
返回日期($ format,$ time); #与您的要求显示格式$格式
}

如果你真的只想打电话(),即为了一致性,您可以将自己的应用程序写入您自己的命名空间,并简单地命名您的自定义功能date()。在这种情况下,PHP的原始date()函数必须调用为\date()。


Is there some way to return all date returned by the date() function to be offset by some specific amount of time ? I tried date-default-timezone-set(), didn't seem to work on the server, so is there any other way?

解决方案

You could also write your own version of the date() function which returns the date processed by the callback you require (adding an offset).

<?php

function date_offset($format, $offset)
{
  $time = time() + $offset; # $offset should be in milliseconds
  return date($format, $time); # with your require display format $format
}

If you really want to just call date(), i.e. for sake of consistency, you could write your app in your own namespace and simply name your custom function date(). In that case, PHP's original date() function has to be called as \date().

这篇关于设置php中的时间偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆