dream1849 2014-05-13 19:19
浏览 154
已采纳

创建2014年5月13日下午3点的时间戳

I have an application that needs to send a UTC timestamp in order for it to work correctly. In my application a user can have any number of timezones. So if they pick 3pm and their timezone is America/New_York, it is a different 3pm than if it was America/Chicago.

I need to figure out a way to change the date into the right UTC timestamp. I know I can use date_default_timezone_set("UTC")...but I don't think will work correctly.

I think I need to calculate a difference between UTC and regular timezone, but I am not sure. Any advice is welcomes.

date_default_timezone_set("UTC");
echo strtotime('5/13/2014 3:00 PM');

1399993200

date_default_timezone_set("America/New_York");
echo strtotime('5/13/2014 3:00 PM');

1400007600

As you can tell these 2 values are different.

EDIT: Here is what my code looks like. It doesn't seem to work correctly as the application doesn't show the event in the right time.

$previous_timezone = date_default_timezone_get();
date_default_timezone_set("UTC");
$aceroute_schedule = $this->sale_lib->get_send_to_aceroute_schedule();

if (($start_time = strtotime($aceroute_schedule['aceroute_schedule_date']. ' '.$aceroute_schedule['aceroute_schedule_time_start'])) !== FALSE)
{
    //Append 000 as as string for 32 bit systems
    $start_epoch = $start_time.'000';
    $end_epoch = strtotime('+ '.$aceroute_schedule['aceroute_duration'].' minutes', $start_time).'000';
}
else //Default to current time + 1 hour
{
        //Append 000 as as string for 32 bit systems
        $start_epoch = time().'000';
        $end_epoch = strtotime('+1 hour', time()).'000';
}

$event->start_epoch = $start_epoch;
$event->end_epoch = $end_epoch;
  • 写回答

2条回答 默认 最新

  • douda5706 2014-05-13 19:27
    关注

    Update:

    This will now create a DateTime object in the user's DateTimeZone ('America/New_York'). And then it will set that object's timezone to UTC. To get the timestamp (or other string representations of date), use ::format().

    # Create NY date
    $NY = new DateTimeZone("America/New_York");
    $NYdate = new DateTime('5/13/2014 3:00 PM', $NY);
    
    # Set timezone to UTC
    $UTC = new DateTimeZone("UTC");
    $UTCdate = $NYdate->setTimezone($UTC);
    
    # Get timestamp (PHP 5.2 compatible)
    $timezone = $UTCdate->format('U');
    var_dump($timezone); // a string containing UNIX timestamp
    

    First I create 2 DateTime objects based off of their respective DateTimeZone objects. Then we can either use OOP ::diff() to get another object containing information about the time difference. Or we can use simple integers representing the difference in seconds from ::getTimestamp.

    $date = '5/13/2014 3:00 PM';
    
    # Create NY date
    $NY = new DateTimeZone("America/New_York");
    $NYdate = new DateTime($date, $NY);
    
    # Create UTC date
    $UTC = new DateTimeZone("UTC");
    $UTCdate = new DateTime($date, $UTC);
    
    # Find difference object
    $diff = $NYdate->diff($UTCdate);
    var_dump($diff); // a DateInterval object containing time difference info
    
    # Find difference in seconds
    $diff = $NYdate->getTimestamp() - $UTCdate->getTimestamp();
    var_dump($diff); // an integer containing time difference in seconds
    

    Links:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?