php 如何在特定时区的php中查找星期几

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/712761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:36:24  来源:igfitidea点击:

How to find day of week in php in a specific timezone

phpdatetimetimezone

提问by trante

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. Unix time stamp calculated by php time() function.
  1. 用户输入的时区
  2. 由 php time() 函数计算的 Unix 时间戳。

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

但我不知道如何继续......我将如何在这个时区获得“星期几”。

回答by Botond Béres

$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

$dw 将是 0(周日)到 6(周六),正如您在此处看到的:http: //www.php.net/manual/en/function.date.php

回答by trante

My solution is this:

我的解决方案是这样的:

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

Output is: Tuesday

输出是: Tuesday

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

Output is: Tue

输出是: Tue

回答by trante

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.

如果您发现任何更正,请报告。

回答by cjohansson

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

我认为这是正确的答案,只需更改Europe/Stockholm为用户时区即可。

$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

回答by Andre

Another quick way:

另一种快捷方式:

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

回答by nickf

If you can get their timezone offset, you can just add it to the current timestamp and then use the gmdatefunction 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);

回答by Kalium

$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)。它的工作方式是存储脚本的当前时区,将其更改为用户指定的时区,从date()指定时间戳的函数中获取星期几,然后将脚本的时区设置回它开始时的时区。

You might have some adventures with timezone identifiers, though.

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

回答by HerdplattenToni

"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

“星期几”实际上是您可以分别从格式为“l”或“N”的 php date() 函数中直接获取的内容。看看 说明书

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

编辑:对不起,我没有正确阅读 Kalium 的帖子,他已经解释过了。我的错。

回答by user2214236

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);
}

回答by Drinkynet

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);
}