...been researching this for hours - and only getting more and more confused!
I'm on the east coast, USA.
My database is hosted by a server that seems to be on the west coast.
I can tell this because when I insert new data into my database at say 9:45am my time (which again, is east coast time), when I look directly into the MySQL table on the server, it shows the data as having been inserted at 6:45am.
Problem is, when my App reads that date back from the server and compares it to its own last-saved time, that last-saved time is always later than the last-update time coming from the server.
So any new data I insert into the server is automatically ignored, because its registering as data that's old (3 hrs. old to be exact.)
I've tried all sorts of conversions using NSDateFormatter
, NSTimeZone
, NSLocale
- and various combinations of all of them, and I'm getting spaghetti mishmash soup that just doesn't work.
Here's some of my code:
PHP code - used when inserting the new data into the MySQL database:
// Declare a var for the date of insertion into the Database:
$storeItemDateAdded = date('Y-m-d H:i:s');
...
// Insert the new data into the database:
$stmt = $mysqli->prepare("INSERT INTO StoreTable (Name, Price, DateAdded)
VALUES ('". $newStoreItemName ."', '". $newStoreItemPrice ."', '".$storeItemDateAdded."');");
Objective-C Code
(Note:
-The JSON object (NSDictionary) coming into the App shows the date in this format: 2015-03-29 06:45:30
-I'm therefore storing this particular part of the JSON in an NSString
var called remoteDBStoreLastUpdateDate
)
// Convert this String to an actual NSDate Object:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *liveStoreActualUpdateNSDate = [dateFormat dateFromString:remoteDBStoreLastUpdateDate];
// Now compare the Saved date and this new Remote date:
if ([storeSavedLastUpdateNSDate compare:liveStoreActualUpdateNSDate] == NSOrderedAscending) {
NSLog(@"Need to get FRESH STORE DATA!");
[self getNewData];
}
else {
NSLog(@"STORE is up to date!");
[self useSavedData];
}
So like I said, this test yields the wrong results - but not because the code/logic is wrong, but because the NSDate
objects I'm comparing are from different time-zones.
So how do I get them to be of the same time-zone and make a true, valid comparison?