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

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

我要做的是: 当用户访问我的页面时,我会询问他的时区,然后在他的时区中显示“每周的一天”。

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

这就是我试图实现它的方式:

  1. 用户输入的时区
  2. 用 php time ()函数计算 Unix 时间戳。

但我不知道该怎么办。 我怎样才能在这个时区得到“一周中的某一天”呢。

295962 次浏览
$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.

You might have some adventures with timezone identifiers, though.

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.

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

"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

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

$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

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.

Another quick way:

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

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

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

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);
}
echo date('l', strtotime('today'));

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)

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

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

Standard letter-based representations of date parts are great, except the fact they're not so intuitive. The much more convenient way is to identify basic abstractions and a number of specific implementations. Besides, with this approach, you can benefir from autocompletion.

Since we're talking about datetimes here, it seems plausible that the basic abstraction is ISO8601DateTime. One of the specific implementations is current datetime, the one you need when a makes a request to your backend, hence Now() class. Second one which is of some use for you is a datetime adjusted to some timezone. Not surprisingly, it's called AdjustedAccordingToTimeZone. And finally, you need a day of the week in a passed datetime's timezone: there is a LocalDayOfWeek class for that. So the code looks like the following:

(new LocalDayOfWeek(
new AdjustedAccordingToTimeZone(
new Now(),
new TimeZoneFromString($_POST['timezone'])
)
))
->value();

For more about this approach, take a look here.