0

I have an offset from UTC stored in minutes: e.g -240

I'm trying to find the corresponding UNIX timestamp of midnight of the current day for this particular offset.

I found similar information in questions like this one: How do I get the UTC time of "midnight" for a given timezone?

However, I don't have the city name/timezone jurisdiction, just a minute offset. I think this should be fine since for my purposes I don't need to account for daylight savings, it can be off by an hour and still be fine.

Examples

Offset: -420

Midnight on 7/12/2014: 1405148400 (unix TS)

With UTC, I would have to first tell if it's the next day or same day as the TZ because it may have a different "last midnight".

4

3 回答 3

1

While this solution looks a little ugly it does do what I think you're asking for! This example uses -180 minutes as the offset.

date_default_timezone_set('UTC');

// Work out which day the time zone is in
$day = strtotime('-180 minutes');

// Strip of the time part of the day, to give UTC midnight on the correct day
$utcMidnight = strtotime('midnight', $day);

// Now apply the offset in reverse to give the zone's midnight
$zoneMidnight = strtotime('+180 minutes', $utcMidnight);
于 2014-07-13T01:38:15.410 回答
0

您可以使用date_default_timezone_set使所有与时间相关的功能都承认这种转变。首先要做的是将这些分钟转换为小时,因为 UTC 间隔在 n 和 n+1 之间为 1 小时。

$hours = $minutes / 60;

我还建议您先检查分钟值:

if($minutes % 60 == 0) // We're good.

现在,如果您想将 UTC 偏移量转换为时区,您可以创建您的函数:

<?php
function offsetToTimezone($offset){
    $timezones = array( 
        "-12" => "Pacific/Kwajalein", 
        "-11" => "Pacific/Samoa", 
        "-10" => "Pacific/Honolulu", 
        "-9" => "America/Juneau", 
        "-8" => "America/Los_Angeles", 
        "-7" => "America/Denver", 
        "-6" => "America/Mexico_City", 
        "-5" => "America/New_York", 
        "-4" => "America/Caracas", 
        "-3.5" => "America/St_Johns", 
        "-3" => "America/Argentina/Buenos_Aires", 
        "-2" => "Atlantic/Azores",
        "-1" => "Atlantic/Azores", 
        "0" => "Europe/London", 
        "1" => "Europe/Paris", 
        "2" => "Europe/Helsinki", 
        "3" => "Europe/Moscow", 
        "3.5" => "Asia/Tehran", 
        "4" => "Asia/Baku", 
        "4.5" => "Asia/Kabul", 
        "5" => "Asia/Karachi", 
        "5.5" => "Asia/Calcutta", 
        "6" => "Asia/Colombo", 
        "7" => "Asia/Bangkok", 
        "8" => "Asia/Singapore", 
        "9" => "Asia/Tokyo", 
        "9.5" => "Australia/Darwin", 
        "10" => "Pacific/Guam", 
        "11" => "Asia/Magadan", 
        "12" => "Asia/Kamchatka" 
    );

    return $timezones[$offset];
}
?>

...并使用 if 进行转换:

date_default_timezone_set(offsetToTimezone($hours));

顺便说一句,我建议你看看这个答案,它为你提供了一种更优雅的方式来实现offsetToTimezone.

现在,如果您的脚本配置在正确的时区,只需询问时间戳:

$timestamp = mktime(0, 0, 0);

如果在某个时候,您需要将时区重置为默认值,您可能需要date_default_timezone_get保存它:

$timezone = date_default_timezone_get();
// Change to another timezone based on your offset.
// Get your timestamp.
date_default_timezone_set($timezone);
于 2014-07-13T00:33:04.220 回答
0

我不得不考虑很多,但我认为这是我正在寻找的解决方案。如果您认为此算法不正确,请告诉我。

function getLastMidnightForOffset( $minuteOffset ) {
    $today = mktime( 0, 0, 0 );
    $tomorrow = $today + 86400;
    $yesterday = $today - 86400;
    $offset = $minuteOffset * 60;

    if ( time() + $offset >= $tomorrow ) {
        $localMidnight = $tomorrow - $offset;
    } elseif ( time() + $offset >= $today ) {
        $localMidnight = $today - $offset;
    } else {
        $localMidnight = $yesterday - $offset;
    }

    return $localMidnight;
}
于 2014-07-13T02:08:17.037 回答