本文介绍了如何在“普通周”(星期日至星期六)中显示转换后的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个日程安排应用程序,其中一个用户可以设置其所有星期的一般可用性,如下所示:

 周日|星期一| ... |星期五|星期六

当我们要求印度的某个人A表示其有空时,我们请他选择从这样的值下拉列表中获取:

  12:00 am 
12:30 am
01: 00am
...
11:30 pm

我们请他选择两者



我们在数据库中保存的只是这些值(请参见以下示例):

  user_id avail_day from to 
1 Sunday 12:00:00 12:15:00
2 Monday 12:00:00 12:15:00

因此,从本质上讲,它看起来像以下(

 (A)
Sunday |星期一| ... |星期五|星期六
-----------------------------------------
| | | |上午8:30到上午10:30

作为单独的一条信息,我们知道他选择了在IST(印度标准时间)工作,该时间目前是格林尼治标准时间+ 5:30小时,因此我们可以假设他选择的值适用于他当前所在的时区。



现在,对于目前在格林尼治标准时间4小时(EDT)的美国东海岸的B人,这次实际上是

 星期五,23:00:00到星期六,01:00:00 

我们需要帮助弄清楚如何:

(a)将IST中人A的早期文本值转换为EST人的本地值(请注意,我们仅知道可用性的日期和时间作为TEXT值)

(b)并且,然后,我们需要弄清楚如何在从星期日开始到星期六结束的标准周中显示它。
我们想要显示的内容应该是这样的:

 (B)
Sunday |星期一| ... |星期五|星期六
--------------------------------------------- -----------------
| | | 11:00 pm至12:00 am |从12:00 am到1:00 am

是否有任何将(A)转换为(B)的聪明方法? / p>




的代码变成通用函数(修订版2)

  //此函数应相对于 from_date使用b $ b // $ from_timebegin和$ from_timeend必须在同一天,而不是转到下一个
函数shift_timezones_onweek3($ from_timezone,$ from_date,$ from_timebegin,$ from_timeend,$ to_timezone)
{
$ tz1 =新的DateTimezone($ from_timezone);

$ datetime1 = new DateTime( $ from_date $ from_timebegin,$ tz1);
$ datetime2 = new DateTime( $ from_date $ from_timeend,$ tz1);

$ interval = $ datetime1-> diff($ datetime2);

$ indiaAvail = array(
array($ datetime1,$ datetime2)
);


$ tz2 =新的DateTimezone($ to_timezone);
//转换周期:
$ times = array_map(
函数(array $ p)use($ tz2){
$ res = array();
foreach ($ p as $ d){
$ res [] = $ d-> setTimezone($ tz2);
}
return $ res;
},
$ india可用
);

$ res = array();
foreach($ times $ t){
$ t1 = reset($ t);
$ t2 = next($ t);
if($ t1-> format( d)== $ t2-> format( d)){
$ res [$ t1-> format( l) ] [] = $ t1-> format( g:ia)。 至 。
$ t2-> format( g:ia);
}
else {
$ res [$ t1-> format( l)] [] = $ t1-> format( g:ia)。 至晚上11:59;
$ res [$ t2-> format( l)] [] = 12:00 am to。 $ t2-> format( g:ia);
}
}

return $ res;
}


解决方案

您的问题没有解决在真空中考虑平日的感觉。这些工作日必须是实际的日子,因为时间转换规则会随着年份(DST)和年份的变化而变化(政客有时会更改时区和/或DST开始/结束的日期)。



也就是说,假设您有一个针对8月第一周的每周可用性计划,这里定义为2010年8月1日至8月7日这一周:

 <?php 
$ tz1 = new DateTimezone( Asia / Calcutta);
$ indiaAvail =数组(
新的DatePeriod(新的DateTime( 2010-08-01 10:00:00,$ tz1),
的新DateInterval( PT2H15M),1) ,
新DatePeriod(新DateTime( 2010-08-07 03:00:00,$ tz1),
新DateInterval( PT8H),1),
);


$ tz2 = new DateTimezone( America / New_York);
//转换周期:
$ times = array_map(
函数(DatePeriod $ p)使用($ tz2){
$ res = array();
foreach ($ p as $ d){
$ res [] = $ d-> setTimezone($ tz2);
}
return $ res;
},
$ india可用
);

$ res = array();
foreach($ times $ t){
$ t1 = reset($ t);
$ t2 = next($ t);
if($ t1-> format( d)== $ t2-> format( d)){
$ res [$ t1-> format( l) ] [] = $ t1-> format( g:ia)。 至 。
$ t2-> format( g:ia);
}
else {
$ res [$ t1-> format( l)] [] = $ t1-> format( g:ia)。 至晚上11:59;
$ res [$ t2-> format( l)] [] = 12:00 am to。 $ t2-> format( g:ia);
}
}

print_r($ res);

给予

 
数组

[星期日] =>数组

[0] => 12:30 am至2:45 am


[星期五] =>数组

[0] => 5:30 pm到11:59 pm


[星期六] =>数组

[0] => 12:00 am至1:30 am



这可能与实际上是不同日期的工作日放在相同的购物篮中,但是如果没有明确指出日期(或添加周六(下周)和周六(前一周)之类的东西,显然无法避免)。不过,这似乎就是您想要的。


We are building a scheduling application wherein one user may set his "general availability" for all weeks like the following:

Sunday | Monday | ... | Friday | Saturday

When we ask a person A in India to indicate his "availability", we ask him to select from a drop down of values something like this:

12:00am
12:30am
01:00am
...
11:30pm

We ask him to select BOTH the "From" time (starting) and the "Till" time (ending).

What we SAVE in the database is JUST these values (see the following example):

user_id avail_day   from        to  
1     Sunday        12:00:00    12:15:00
2     Monday        12:00:00    12:15:00

So, in essence, it looks like the following (in his LOCAL time zone)

(A)
Sunday | Monday | ... | Friday | Saturday
-----------------------------------------
       |        |     |        | 8:30am to 10:30am

As a separate piece of information, we know that he has selected to work in the IST (Indian Standard Time), which is presently GMT + 5:30 hours, so we can assume that the values he chooses are FOR the time zone he's presently in.

Now, for a person B on the East Coast, which is presently GMT - 4 hours (EDT), this time would be actually

Friday, 23:00:00 to Saturday, 01:00:00

We need help in figuring out how to:
(a) convert the earlier "text value" of the person A in IST to the local value of the EST person (NOTE that we know JUST the day and hours of availability as TEXT values)
(b) AND, then, we need to figure out how to display it on a "Standard week" beginning on a Sunday and ending on a Saturday.What we want displayed should be something like this:

(B)
Sunday | Monday | ... |       Friday       |      Saturday
--------------------------------------------------------------
       |        |     | 11:00pm to 12:00am | 12:00am to 1:00am

Any smart ways of converting (A) into (B)?


Artefacto's code made into a generic function (Revision 2)

// This function should be used relative to a "from_date"
// The $from_timebegin and $from_timeend MUST be for the same day, not rolling over to the next
function shift_timezones_onweek3($from_timezone, $from_date, $from_timebegin, $from_timeend, $to_timezone)
{
    $tz1 = new DateTimezone($from_timezone);

    $datetime1 = new DateTime("$from_date $from_timebegin", $tz1);
    $datetime2 = new DateTime("$from_date $from_timeend", $tz1);

    $interval = $datetime1->diff($datetime2);

    $indiaAvail = array(
        array($datetime1, $datetime2)
    );


    $tz2 = new DateTimezone($to_timezone);
    //convert periods:
    $times = array_map(
        function (array $p) use ($tz2) {
           $res = array();
           foreach ($p as $d) {
               $res[] = $d->setTimezone($tz2);
           }
           return $res;
        },
        $indiaAvail
    );

    $res = array();
    foreach ($times as $t) {
        $t1 = reset($t);
        $t2 = next($t);
        if ($t1->format("d") == $t2->format("d")) {
            $res[$t1->format("l")][] = $t1->format("g:ia") . " to ".
                $t2->format("g:ia");
        }
        else {
            $res[$t1->format("l")][] = $t1->format("g:ia") . " to 11:59pm";
            $res[$t2->format("l")][] = "12:00am to ". $t2->format("g:ia");
        }
    }

    return $res;
}
解决方案

Your question doesn't make sense considering weekdays in the vacuum. These weekdays must be actual days, because the time conversion rules change along the year (DST) and through the years (politicians sometimes change the timezones and/or the date in which DST starts/ends).

That said, let's say you have you have a week availability plan for the first week of August, here defined as the week Aug 1 to Aug 7 2010:

<?php
$tz1 = new DateTimezone("Asia/Calcutta");
$indiaAvail = array(
    new DatePeriod(new DateTime("2010-08-01 10:00:00", $tz1),
        new DateInterval("PT2H15M"), 1),
    new DatePeriod(new DateTime("2010-08-07 03:00:00", $tz1),
        new DateInterval("PT8H"), 1),
);


$tz2 = new DateTimezone("America/New_York");
//convert periods:
$times = array_map(
    function (DatePeriod $p) use ($tz2) {
       $res = array();
       foreach ($p as $d) {
           $res[] = $d->setTimezone($tz2);
       }
       return $res;
    },
    $indiaAvail
);

$res = array();
foreach ($times as $t) {
    $t1 = reset($t);
    $t2 = next($t);
    if ($t1->format("d") == $t2->format("d")) {
        $res[$t1->format("l")][] = $t1->format("g:ia") . " to ".
            $t2->format("g:ia");
    }
    else {
        $res[$t1->format("l")][] = $t1->format("g:ia") . " to 11:59pm";
        $res[$t2->format("l")][] = "12:00am to ". $t2->format("g:ia");
    }
}

print_r($res);

gives

Array
(
    [Sunday] => Array
        (
            [0] => 12:30am to 2:45am
        )

    [Friday] => Array
        (
            [0] => 5:30pm to 11:59pm
        )

    [Saturday] => Array
        (
            [0] => 12:00am to 1:30am
        )

)

This may put in the same basket weekdays that are actually different days, but there's obviously no way to avoid it without explicitly indicating the day (or adding something like "Saturday (week after)" and "Saturday (week before)". This appears to be what you want, though.

这篇关于如何在“普通周”(星期日至星期六)中显示转换后的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 08:54