在Xcode 6.4中从服务器获取当前日期和时间时出现问题


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

为了避免任何日期和时间问题,我计划从自己的server中获取日期。因此,我将这段代码写在PHP文件中,并将该文件上传到我的服务器中。

日期.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] ;