Zeeshan Rang
Zeeshan Rang

Reputation: 19905

How to convert timestamp, to the country specific time, where it is being fetched from.

I am working on an app, which has an option to upload images (pretty much the same way instagram does). The way the database is designed is, every time a row is created in the database for the uploaded image, I save the timestamp of the row created. This date is send back to the mobile app, when a user is trying to fetch the uploaded image.

So the issue I am facing now is, the date created is the date specific to the country where ever the server is located (right now I am using Amazon server). And if I upload an image right now, and try to fetch it immediately, it shows me it was uploaded something like 6 hrs back.

The one of solution I thou about this was to, send the GPS location along with the uploading image. But the issue with this is, iPhone apps, asks the users if they allow the app to use GPS. So, what if the user denies that. Also the app is for android, and it's a possibility that some phone might not even have GPS built-in.

I am using php for my backend code, and mysql database.

Thanks in advance. Zeeshan

Upvotes: 1

Views: 1127

Answers (3)

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Try this will work for UTC timestamp

public  String getDateStringCurrentTimeZone(long timestamp) {

        Calendar calendar = Calendar.getInstance();
        TimeZone t = TimeZone.getDefault();

        calendar.setTimeInMillis(timestamp * 1000);
        calendar.add(Calendar.MILLISECOND, t.getOffset(calendar.getTimeInMillis()));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = sdf.format(calendar.getTime());
        return dateString;
    }

Upvotes: 0

Carl Groner
Carl Groner

Reputation: 4359

You would make your job much simpler by always storing UTC times instead of local ones, then converting it to the appropriate time zone when you display it to the user.

That said... when fetching the time from mysql you can use the convert_tz mysql function to convert the times from the server timezone to UTC. Send this UTC time to the client and convert it to the local timezone of the user.

Upvotes: 2

flx
flx

Reputation: 14226

Typically you save timestamps in your backend with UTC.

When displaying the time, you use the local time zone of the device to show the correct local time.

Upvotes: 2

Related Questions