Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:获取指定时间所在的时区_Php - Fatal编程技术网

PHP:获取指定时间所在的时区

PHP:获取指定时间所在的时区,php,Php,我有一个小时的时间范围,比如2015-11-23 15:00:00到2015-11-23 16:00:00。我想知道当前时间在这个时间范围内的哪个时区 所以,如果我可以通过SQL查询让您理解,我会编写如下代码 从时间

我有一个小时的时间范围,比如2015-11-23 15:00:00到2015-11-23 16:00:00。我想知道当前时间在这个时间范围内的哪个时区

所以,如果我可以通过SQL查询让您理解,我会编写如下代码

从时间<'2015-11-23 15:00:00'和时间与时间<'2015-11-23 15:00:00'的所有_时区中选择时区


用PHP做这件事的最好方法是什么

这将为脚本中的所有日期/时间提供当前时区:

date_default_timezone_get();
示例代码:

<?php
echo date_default_timezone_get();
?>
注意:下面的示例显示了设置时区将如何根据时区设置为您提供日期/时间,并响应评论中的澄清请求:

<?php
date_default_timezone_set('America/Denver');
echo date(DATE_RFC2822);
?>

HTH

首先,您的脚本默认设置了时区,您可以这样找到它

echo date_default_timezone_get() . ' => ' . date('e') . ' => ' . date('T');
或者只是:

echo date_default_timezone_get();
您可以使用不同的时区 更多信息可在手册中找到

*编辑:* 您必须使用dateTime对象才能达到此目的

获取所有时区及其偏移量的小示例:

$timezone_identifiers = DateTimeZone::listIdentifiers();


foreach($timezone_identifiers as $tz) {
    $tz = new DateTimeZone("$tz");
    $transitions = $tz->getTransitions();
    print_r(array_slice($transitions, 0, 3));
}
如果你知道2015-11-23 15:00:00的时区是默认utc,我想你可以很容易地计算出来


希望能有所帮助。

谢谢大家的回答。基本上这就是我想要的:

function time_exists($from_date, $to_date)
{
    $lowlimit  = (strtotime($from_date) + date('Z') - time())/3600;
    $highlimit = (strtotime($to_date) + date('Z') - time())/3600;

    // $to_date must be higher than $from_date
    // $lowlimit has to be higher than -8 hours
    // $highlimit has to be lower than +14 hours

    if($lowlimit > -11 && $highlimit < 14){
       return true;
    }        
    return false;       
}

$lowlimit和$highlimit变量还表示GMT中的时区。

这将为脚本中的所有日期/时间提供当前时区-抱歉,我没有得到它。脚本中的所有日期/时间是什么意思?你能举个例子吗?对!我想做的就是这样。从时间<'2015-11-23 15:00:00'和时间与时间<'2015-11-23 15:00:00'的所有_时区中选择时区;别误会我。这与SQL无关。只是想让事情变得更容易。
function time_exists($from_date, $to_date)
{
    $lowlimit  = (strtotime($from_date) + date('Z') - time())/3600;
    $highlimit = (strtotime($to_date) + date('Z') - time())/3600;

    // $to_date must be higher than $from_date
    // $lowlimit has to be higher than -8 hours
    // $highlimit has to be lower than +14 hours

    if($lowlimit > -11 && $highlimit < 14){
       return true;
    }        
    return false;       
}