简体   繁体   English

PHP日期时区转换-天

[英]PHP date time zone conversion - days

I have date conversion issue: The goal is to determine an "order by date" for a book, this should be 13 days prior to the book's "release date", which is in EST. 我有日期转换问题:目的是确定书籍的“按日期排序”,该日期应早于EST的书籍“发行日期”之前13天。 The "order by date" should display the 13 day time frame plus any time diff between the user's time and EST (New York) time. “按日期排序”应显示13天的时间范围以及用户时间与EST(纽约)时间之间的任何时差。 So in my function below, I'm getting the release date, NYC time, user's time and trying to do order_by_date = release_date - ( (nyc/user local time diff) + 13 days). 因此,在下面的函数中,我要获取发布日期,NYC时间,用户时间,并尝试执行order_by_date = release_date-((nyc /用户本地时间差)+ 13天)。 It seemed to be working, but after testing this out with multiple release dates, I'm consistently returning a 14 day difference, not a 13 day one... My main question is why would the function below output a date that is 14 days before a release date and not 13 days? 它似乎正在运行,但是在使用多个发布日期进行测试之后,我始终返回14天的差异,而不是13天的差异。我的主要问题是,为什么下面的函数会输出14天的日期在发布日期之前而不是13天之前? I've tried echoing each time variable and each one looks normal (ie for a user in NYC, the time diff is 0, but for someone on PST it's 3hour diff), I wonder if the formatting is having an effect on the value? 我试过回显每个时间变量,并且每个变量看起来都很正常(例如,对于纽约市的用户,时间差异为0,但对于PST上的某个人来说,则为3小时差异),我想知道格式是否对值有影响? Thanks for any input: 感谢您的任何输入:

function get_order_by_date( ) {
        $release_date = '26-02-2019 00:00:00'
        $ny_timezone = new \DateTimeZone( 'America/New_York' );
        $gmt_timezone = new \DateTimeZone( 'GMT' );
        $user_date_time = new \DateTime( $release_date, $gmt_timezone );
        $offset = $ny_timezone->getOffset( $user_date_time );
        $my_interval = \DateInterval::createFromDateString( (string) $offset . 'seconds' );
        $user_date_time->add( $my_interval );
        $result = $user_date_time->format( 'd-m-Y H:i:s' );
        $order_by_date = date( 'F jS', strtotime( $result . ' - 13 days' ) );

        return $order_by_date;
    }

It might be easier to see why we get a certain date as a result if we simplify the process a little. 如果稍微简化一下流程,可能会更容易理解为什么我们得到某个日期。 If I understand correctly the function needs to take the release date and do two things to it: 如果我正确理解该功能,则需要确定发布日期并对它做两件事:

  1. Shift 13 days prior to it 提前13天移班
  2. Set it to the user's timezone 将其设置为用户的时区

If we start with the release date in the release timezone, making those modifications is more straightforward. 如果我们以发布时区中的发布日期开始,则进行这些修改会更简单。

For the purposes of the answer I'm returning the result in a format that includes the time so we can see exactly where those modifications put the result, but you can use whatever format is needed. 出于答案的目的,我以包含时间的格式返回结果,以便我们可以准确看到这些修改将结果放置在何处,但是您可以使用所需的任何格式。

<?php

function get_order_by_date(string $release_date, string $user_timezone)
{
    $release_timezone = new \DateTimeZone( 'America/New_York' );
    $user_timezone = new \DateTimeZone($user_timezone);

    // start with the release date in NY time
    $orderby_date = new \DateTime($release_date, $release_timezone);

    // 13 days prior
    $orderby_date->modify('-13 days');

    // shift to the user's timezone
    $orderby_date->setTimezone($user_timezone);

    return $orderby_date->format('Y-m-d H:i:s');
}

Using the date in your example, 26-02-2019 00:00:00 moving thirteen days before would give you 13-02-2019 00:00:00 . 在您的示例使用日期, 26-02-2019 00:00:00移动13天之前会给你13-02-2019 00:00:00

At that time in NY, the time in LA would be three hours earlier, so the result would be in the previous day 当时在纽约,在洛杉矶的时间要早​​三小时,所以结果将在前一天

echo get_order_by_date('26-02-2019 00:00:00', 'America/Los_Angeles');  // 2019-02-12 21:00:00

And the time in GMT would be five hours later, so the result would be in the same day 而格林尼治标准时间将是五个小时之后,因此结果将在同一天

echo get_order_by_date('26-02-2019 00:00:00', 'GMT');  // 2019-02-13 05:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM