如何在php中找到一个特定的时区

时间:2022-10-21 12:49:45

I am confused while using php to handle date/time.

我在使用php处理日期/时间时感到困惑。

What I am trying to do is this: When a user visits my page I am asking his timezone and then displaying the 'day of week' in his timezone.

我想做的是:当用户访问我的页面时,我询问他的时区,然后在他的时区显示“星期几”。

I don't want to use the browser's day. I want to do this calculation in php.

我不想用浏览器。我想用php做这个计算。

This is how I am trying to achieve it:

这就是我努力实现它的方式:

  1. The timezone entered by user
  2. 用户输入的时区
  3. Unix time stamp calculated by php time() function.
  4. 使用php时间()函数计算Unix时间戳。

But I dont know how to proceed... How would i get the 'day of week' in this timezone.

但我不知道如何继续……我怎样才能在这个时区得到“星期”呢?

11 个解决方案

#1


139  

$dw = date( "w", $timestamp);

Where $dw will be 0 (for Sunday) through 6 (for Saturday) as you can see here: http://www.php.net/manual/en/function.date.php

您可以在这里看到:http://www.php.net/manual/en/function.date.php

#2


111  

My solution is this:

我的解决方案是:

$tempDate = '2012-07-10';
echo date('l', strtotime( $tempDate));

Output is: Tuesday

输出:周二

$tempDate = '2012-07-10';
echo date('D', strtotime( $tempDate));

Output is: Tue

输出:星期二

#3


20  

Thanks a lot guys for your quick comments.

谢谢你们的快速评论。

This is what i will be using now. Posting the function here so that somebody may use it.

这就是我现在要用的。在这里张贴这个功能,以便有人可以使用它。

public function getDayOfWeek($pTimezone)
{

    $userDateTimeZone = new DateTimeZone($pTimezone);
    $UserDateTime = new DateTime("now", $userDateTimeZone);

    $offsetSeconds = $UserDateTime->getOffset(); 
    //echo $offsetSeconds;

    return gmdate("l", time() + $offsetSeconds);

}

Report if you find any corrections.

如果发现有任何错误,报告。

#4


11  

I think this is the correct answer, just change Europe/Stockholm to the users time-zone.

我认为这是正确的答案,只要把欧洲/斯德哥尔摩改成用户时区即可。

$dateTime = new \DateTime(
    'now',
    new \DateTimeZone('Europe/Stockholm')
);
$day = $dateTime->format('N');

ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday)

ISO-8601周的数字表示(在PHP 5.1.0中添加)1(周一)到7(周日)

http://php.net/manual/en/function.date.php

http://php.net/manual/en/function.date.php

For a list of supported time-zones, see http://php.net/manual/en/timezones.php

有关支持的时区列表,请参见http://php.net/manual/en/timezones.php

#5


8  

Another quick way:

另一个快速的方法:

date_default_timezone_set($userTimezone);
echo date("l");

#6


5  

If you can get their timezone offset, you can just add it to the current timestamp and then use the gmdate function to get their local time.

如果您可以获得它们的时区偏移量,那么只需将它添加到当前的时间戳中,然后使用gmdate函数获取它们的本地时间。

// let's say they're in the timezone GMT+10
$theirOffset = 10;  // $_GET['offset'] perhaps?
$offsetSeconds = $theirOffset * 3600;
echo gmdate("l", time() + $offsetSeconds);

#7


3  

$myTimezone = date_default_timezone_get();
date_default_timezone_set($userTimezone);
$userDay = date('l', $userTimestamp);
date_default_timezone_set($myTimezone);

This should work (didn't test it, so YMMV). It works by storing the script's current timezone, changing it to the one specified by the user, getting the day of the week from the date() function at the specified timestamp, and then setting the script's timezone back to what it was to begin with.

这应该起作用(没有测试,所以是YMMV)。它通过存储脚本的当前时区、将其更改为用户指定的时区、在指定的时间戳中获取日期()函数的星期日,然后将脚本的时区设置为开始时的时区。

You might have some adventures with timezone identifiers, though.

不过,您可能会使用时区标识符进行一些冒险。

#8


2  

"Day of Week" is actually something you can get directly from the php date() function with the format "l" or "N" respectively. Have a look at the manual

“星期之日”实际上是可以直接从php date()函数中获得的,该函数的格式分别为“l”或“N”。看一下说明书好吗

edit: Sorry I didn't read the posts of Kalium properly, he already explained that. My bad.

编辑:对不起,我没有读懂卡利姆的帖子,他已经解释过了。我的坏。

#9


2  

Check date is monday or sunday before get last monday or last sunday

检查日期在上星期一或星期天之前

 public function getWeek($date){
    $date_stamp = strtotime(date('Y-m-d', strtotime($date)));

     //check date is sunday or monday
    $stamp = date('l', $date_stamp);      
    $timestamp = strtotime($date);
    //start week
    if(date('D', $timestamp) == 'Mon'){            
        $week_start = $date;
    }else{
        $week_start = date('Y-m-d', strtotime('Last Monday', $date_stamp));
    }
    //end week
    if($stamp == 'Sunday'){
        $week_end = $date;
    }else{
        $week_end = date('Y-m-d', strtotime('Next Sunday', $date_stamp));
    }        
    return array($week_start, $week_end);
}

#10


2  

Based on one of the other solutions with a flag to switch between weeks starting on Sunday or Monday

基于另一种解决方案,在周日或周一开始的几周之间有一个标志进行切换

function getWeekForDate($date, $weekStartSunday = false){

    $timestamp = strtotime($date);

    // Week starts on Sunday
    if($weekStartSunday){
        $start = (date("D", $timestamp) == 'Sun') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Last Sunday', $timestamp));
        $end = (date("D", $timestamp) == 'Sat') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Next Saturday', $timestamp));
    } else { // Week starts on Monday
        $start = (date("D", $timestamp) == 'Mon') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Last Monday', $timestamp));
        $end = (date("D", $timestamp) == 'Sun') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Next Sunday', $timestamp));
    }

    return array('start' => $start, 'end' => $end);
}

#11


-2  

echo date('l', strtotime('today'));

#1


139  

$dw = date( "w", $timestamp);

Where $dw will be 0 (for Sunday) through 6 (for Saturday) as you can see here: http://www.php.net/manual/en/function.date.php

您可以在这里看到:http://www.php.net/manual/en/function.date.php

#2


111  

My solution is this:

我的解决方案是:

$tempDate = '2012-07-10';
echo date('l', strtotime( $tempDate));

Output is: Tuesday

输出:周二

$tempDate = '2012-07-10';
echo date('D', strtotime( $tempDate));

Output is: Tue

输出:星期二

#3


20  

Thanks a lot guys for your quick comments.

谢谢你们的快速评论。

This is what i will be using now. Posting the function here so that somebody may use it.

这就是我现在要用的。在这里张贴这个功能,以便有人可以使用它。

public function getDayOfWeek($pTimezone)
{

    $userDateTimeZone = new DateTimeZone($pTimezone);
    $UserDateTime = new DateTime("now", $userDateTimeZone);

    $offsetSeconds = $UserDateTime->getOffset(); 
    //echo $offsetSeconds;

    return gmdate("l", time() + $offsetSeconds);

}

Report if you find any corrections.

如果发现有任何错误,报告。

#4


11  

I think this is the correct answer, just change Europe/Stockholm to the users time-zone.

我认为这是正确的答案,只要把欧洲/斯德哥尔摩改成用户时区即可。

$dateTime = new \DateTime(
    'now',
    new \DateTimeZone('Europe/Stockholm')
);
$day = $dateTime->format('N');

ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday)

ISO-8601周的数字表示(在PHP 5.1.0中添加)1(周一)到7(周日)

http://php.net/manual/en/function.date.php

http://php.net/manual/en/function.date.php

For a list of supported time-zones, see http://php.net/manual/en/timezones.php

有关支持的时区列表,请参见http://php.net/manual/en/timezones.php

#5


8  

Another quick way:

另一个快速的方法:

date_default_timezone_set($userTimezone);
echo date("l");

#6


5  

If you can get their timezone offset, you can just add it to the current timestamp and then use the gmdate function to get their local time.

如果您可以获得它们的时区偏移量,那么只需将它添加到当前的时间戳中,然后使用gmdate函数获取它们的本地时间。

// let's say they're in the timezone GMT+10
$theirOffset = 10;  // $_GET['offset'] perhaps?
$offsetSeconds = $theirOffset * 3600;
echo gmdate("l", time() + $offsetSeconds);

#7


3  

$myTimezone = date_default_timezone_get();
date_default_timezone_set($userTimezone);
$userDay = date('l', $userTimestamp);
date_default_timezone_set($myTimezone);

This should work (didn't test it, so YMMV). It works by storing the script's current timezone, changing it to the one specified by the user, getting the day of the week from the date() function at the specified timestamp, and then setting the script's timezone back to what it was to begin with.

这应该起作用(没有测试,所以是YMMV)。它通过存储脚本的当前时区、将其更改为用户指定的时区、在指定的时间戳中获取日期()函数的星期日,然后将脚本的时区设置为开始时的时区。

You might have some adventures with timezone identifiers, though.

不过,您可能会使用时区标识符进行一些冒险。

#8


2  

"Day of Week" is actually something you can get directly from the php date() function with the format "l" or "N" respectively. Have a look at the manual

“星期之日”实际上是可以直接从php date()函数中获得的,该函数的格式分别为“l”或“N”。看一下说明书好吗

edit: Sorry I didn't read the posts of Kalium properly, he already explained that. My bad.

编辑:对不起,我没有读懂卡利姆的帖子,他已经解释过了。我的坏。

#9


2  

Check date is monday or sunday before get last monday or last sunday

检查日期在上星期一或星期天之前

 public function getWeek($date){
    $date_stamp = strtotime(date('Y-m-d', strtotime($date)));

     //check date is sunday or monday
    $stamp = date('l', $date_stamp);      
    $timestamp = strtotime($date);
    //start week
    if(date('D', $timestamp) == 'Mon'){            
        $week_start = $date;
    }else{
        $week_start = date('Y-m-d', strtotime('Last Monday', $date_stamp));
    }
    //end week
    if($stamp == 'Sunday'){
        $week_end = $date;
    }else{
        $week_end = date('Y-m-d', strtotime('Next Sunday', $date_stamp));
    }        
    return array($week_start, $week_end);
}

#10


2  

Based on one of the other solutions with a flag to switch between weeks starting on Sunday or Monday

基于另一种解决方案,在周日或周一开始的几周之间有一个标志进行切换

function getWeekForDate($date, $weekStartSunday = false){

    $timestamp = strtotime($date);

    // Week starts on Sunday
    if($weekStartSunday){
        $start = (date("D", $timestamp) == 'Sun') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Last Sunday', $timestamp));
        $end = (date("D", $timestamp) == 'Sat') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Next Saturday', $timestamp));
    } else { // Week starts on Monday
        $start = (date("D", $timestamp) == 'Mon') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Last Monday', $timestamp));
        $end = (date("D", $timestamp) == 'Sun') ? date('Y-m-d', $timestamp) : date('Y-m-d', strtotime('Next Sunday', $timestamp));
    }

    return array('start' => $start, 'end' => $end);
}

#11


-2  

echo date('l', strtotime('today'));