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

标签 php ios objective-c nsdate

为了避免任何日期和时间问题,我计划从我自己的服务器获取日期。 所以我在 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];

Not the main Root Cause

我的系统日期设置为当前日期和时间。现在,当我调试我得到的代码时:

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] ;

关于php - 在 Xcode 6.4 中从服务器获取当前日期和时间时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34491985/

相关文章:

用于发送邮件的 PHP 脚本在通过浏览器执行时不起作用,但在从命令行执行时工作正常

objective-c - UIScrollView subview 容器错误地更改框架

iphone - iPad 应用程序不保留其尺寸 - 恢复为 iPhone 尺寸

objective-c - Objective-C 和 Swift 之间的 UserDefaults

objective-c - 如何阻止鼠标拖动事件移动整个窗口? [ cocoa ]

php - Android 中的 JSON 异常与服务器端的 PHP 代码

php - 从 SQL 数据库中选择数据并在表中显示不起作用

php - 防止重复数据进入mysql数据库

ios - 在 iOS 中按日期排序

objective-c - 如何为填充有图像的 UITableViewCell 恢复圆角?