0

...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?

sirab333
  • 3,662
  • 8
  • 41
  • 54
  • You should look into this answer: http://stackoverflow.com/a/18124778/4716039 – Peter Lendvay Mar 29 '15 at 15:09
  • 1
    Send the date/time to the server, do not rely on the server's local time. Or use UTC time on all systems which is the default NSDate internal storage. Or add the timezone offset to the time string representation. – zaph Mar 29 '15 at 15:14
  • @Zaph - I thought I _was_ sending the date/time to the server. Isn't that what the PHP script (which I included in my question) doing? Its declaring a variable which captures the date: `$storeItemDateAdded = date('Y-m-d H:i:s');` I then use that var in my SQL statement: `INSERT INTO StoreTable (Name, Price, ... DateAdded) VALUES ('". $newStoreItemName ."', '". $newStoreItemPrice ."',.... '".$storeItemDateAdded."');` So that's sending the date/time to the server. – sirab333 Mar 29 '15 at 15:19

1 Answers1

1

The PHP statement
$storeItemDateAdded = date('Y-m-d H:i:s');
is using the server time. The date() function formats a local date and time, and returns the formatted date string.

Then

$stmt = $mysqli->prepare("INSERT INTO StoreTable (Name, Price, DateAdded) VALUES ('". $newStoreItemName ."', '". $newStoreItemPrice ."', '".$storeItemDateAdded."');");

inserts the server local date/time in the database, not a date/tine sent to the server.

Send the date/time to the server, do not rely on the server's local time. Or use UTC time on all systems which is the default NSDate internal storage. Or add the timezone offset to the time string representation.

Sending the time to the server means get the time on the iOS device, formatting properly and transmitting (sending) it to the server along with the data being sent. Then on the server using the time received from the app as the time in the MySQL statement.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • ok, interesting - I didn't know about that whole `PHP date()` thing and what its really doing. I don't actually know PHP (can you tell?) I'm kinda cobbling things together by googling... cause the boss - in his infinite wisdom - decided I'm the man for the job :-) Good times. Seems like there are multiple options for tackling this whole time-zone business - I'll explore get back to you. – sirab333 Mar 29 '15 at 15:47
  • I added the following at the top of my PHP file: `date_default_timezone_set('EST');` and its _almost_ working correctly. Its now off by 1hr. - because of daylight savings. I next tried changing `EST` to `DST` or `EDT`, but neither does. Isn't there a PHP code for eastern daylight savings time? – sirab333 Mar 29 '15 at 16:54
  • 1
    Still the best choice is UTC, how will this work for users in another timezone, say PDT? Daylight savings time changes on different dates in Europe, does not change in several US States and does not exist in most of the world. Also, `NSDate` internally used UTC (which they call GMT). – zaph Mar 29 '15 at 17:53