簡體   English   中英

在Xcode 6.4中從服務器獲取當前日期和時間時出現問題

[英]Issue while getting current date and time from Server in Xcode 6.4

為了避免任何日期和時間問題,我計划從自己的server獲取日期。 因此,我將這段代碼寫在一個PHP文件中,並將該文件上傳到我的服務器中。

Date.php

<?php

  date_default_timezone_set('Asia/Kolkata');
   $date = date('d/m/Y', time());
    echo $date;

?>

現在要使用它,請使用以下代碼:

 NSURL *url = [NSURL URLWithString:@"http://www.myserver.com/date.php"];
NSError* error;
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

NSDate *serverDate = [dateFormat dateFromString:content];

NSDate *DeviceDate = [NSDate date];

不是主要的根本原因

我的系統日期設置為當前日期和時間。 現在,當我調試代碼時,我得到:

serverDate = 2015-12-27 18:30:00 +0000

deviceDate = 2015-12-28 10:35:16 +0000

我不明白為什么這兩個日期之間有一天的差異。 我缺少什么?

在此函數中傳遞兩個日期。 現在是UTC的返回日期。

NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:YourDate];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:YourDate];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:YourDate] ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM