I am developing a laravel application which stores GMT time in database accross the world.
Now I want user to see time in his local timezone but for this I must aware of user's local timezone which I don't.
I have user's country ISO code and using DateTimeZone::listIdentifiers method I can get the timezone but it returns array of timezones for some countries.
So is there a method or way through which I can get offset in hours between GMT +00:00 and user's local GMT time?
You need to make some request passing the timezone from client/browser to your server to get an accurate timezone.
Many countries have multiple timezones, Russia, USA, just to mention some. So country code is not enough.
Then, don't use offset, use named timezone as many timezones also have multiple offset due to daylight saving. Europe/Paris can be GMT+1 or GMT+2 and so current offset between GMT+0 now != current offset for your given timestamp.
When you have the precise city name of the timezone, then it's really simple to use it before formatting a Carbon date:
Carbon::createFromTimestamp($utcTimestamp)->tz('America/New_York')
Related
I'm trying to compare a start date of an event resource I have with the now date. All dates are being stored as UTC and then the timezone is set according to the user's timezone.
But when I compare the dates the difference is always the same regardless of the timezone I set. It's always being evaluated as UTC. So for instance when I set
$now = Carbon::now()->tz($profileTimezone);
And then try to compare it with another date
$difference = $now->diffInHours($event->starts_at));
It's always returning the same difference object with the same values regardless of the timezone I set for now. Shouldn't the difference in hours, for example, change when now is in a different timezone?
When running the tz method it will convert the datetime object to that timezone. Meaning if you change now to a timezone which is 1 hour behind from the current timezone, it will subtract that hour from the time.
What you are looking for is the method shiftTimezone, that one will change the timezone without changing the time also.
Carbon::now();
// 2019-07-29 12:53:29.575769 UTC (+00:00)
Carbon::now()->shiftTimezone('Asia/Phnom_Penh');
// 2019-07-29 12:53:29.572207 Asia/Phnom_Penh (+07:00)
Carbon::now()->tz('Asia/Phnom_Penh');
// 2019-07-29 19:53:29.575776 Asia/Phnom_Penh (+07:00)
I'm trying to change some old .asp files with vbs. Our database is going to be converted to store dates in UTC, but on webpages it should show dates and time in "Europe/Helsinki" timezone(
TimeZoneInfo.FindSystemTimeZoneById("FLE Standard Time")
in c#). How can I cast the UTC date I get from db query( the query is run in the .asp file as well and the result put into table) to correct date time using vbscript?
Just offset the UTC dates using DateAdd().
Const EETOffset = -2 'EET offset from UTC is -2 hours
Dim dbDateValue 'Assumed value from DB
Dim newDate
'... DB process to populate dbDateValue
newDate = DateAdd("h", EETOffset, dbDateValue)
Note: One problem with this approach is you will also have to compensate for EET and EEST (Eastern European Summer Time) manually based on the time of year. Which is also more difficult when you take into consideration some places don't use it and use EET all year round instead.
See EET – Eastern European Time (Standard Time).
Depending on the RDMS you are using you should even be able to manipulate the dates before they get to the page as part of the initial query.
Useful Links
Format current date and time
How to format a datetime with minimal separators and timezone in VBScript?
This is what i'm trying to do:
I want to create a simple shell script that checks the current timezone in the UK.
i.e. BST or GMT.
I only can display the time for the current timezone the UK is in. i.e. UK is in GMT right now and I can only display that time. [TZ=GMT date]
Please note: I do not wish to permanently modify the UNIX time zone on the server (currently CET)
Based on that I need to do some calculations (which I'm fine with)
I have already searched and I cannot find anything specific to this problem. Thank you for your help
To get the date for a particular timezone, you can do:
TZ=GMT date
(Or date +%s if you want epoch format, which is also TZ independent, but altogether friendlier for calculations. ).
For what it is now, relative time I think it's as simple as:
TZ=Europe/London date
Which I think should cause your system to report BST/GMT appropriately.
If you want it to specifically report the offset, you can use the %z format specifier:
TZ=Europe/London date +"%Y%m%d %H:%M:%S %z"
I am currently storing events of some entities in UTC time but I am not sure if I should do that in this case. Imagine there's an event at 10pm local time (-4h UTC) and a mobile App fetches "todays events". This could e.g. look like this:
App sends request to fetch all clubs in the near location
After receiving all clubs it sends a request to get all events for today. It therefore sends the local time Sun. 10pm to the server.
The server would convert the local time of the mobile device to UTC Mon. 1am and fetch all events from Monday. But of course that was not what I wanted.
Fetching all events from the clubs and convert them to their local time using their local time offset information is not really a great solution.
So wouldn't it be better to just store all events in local time? In that case the mobile App would send its local time to the server which would be able to query all events from the clubs in local time as well.
This sounds much simpler to me but I am not sure if I overlook something.
So what would I do in this case?
Yes, storing everything in UTC is probably the best solution.
You don't say how you are "storing" the dates/times, but if you are using Dates or Joda equivalents, then you should know that their underlying representation is effectively in UTC (they represent a moment in time as an offset in milliseconds since the "Epoch", which is Midnight, Jan 1, 1970 UTC). These dates only have a timezone when you format them as Strings.
Most databases do something similar (store the date in a common timezone, usually UTC). The major exception that I've found is the generally available date-time related column types in MS SqlServer which by default store everything in the local timezone of the server.
Also be aware that if you use SQLite, and you store a date/time by passing a String in SQL that contains a timezone, SQLite will store it without warning, but will ignore the timezone and assume that the timezone is UTC, giving you a result other than what you might expect.
For more on this, see my (old) blog post at http://greybeardedgeek.net/2012/11/24/java-dates/
The other answer is correct. Some more thoughts here.
A time zone is more than the offset from UTC mentioned in the Question. A time zone is also the set of past, present, and future rules for anomalies such as Daylight Saving Time. You should refer to a time zone by its proper name, continent plus Slash plus city or region. Never use the 3-4 letter codes such as EST or IST.
To search for events in the user's "today", you must know the user’s time zone. For example, a new day dawns earlier in Paris than in Montréal. After the stroke of midnight in Paris we still have a few hours of “yesterday” left to go in Montréal.
While you can make a guess as to the user’s time zone, the most reliable way is to ask the user.
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTimeZone now = DateTimeZone.now( zone );
DateTime today = now.withTimeAtStartOfDay();
DateTime tomorrow = today.plusDays( 1 );
// Search for events that start >= today AND that start < tomorrow.
To search Joda-Time objects, use the Comparator built into DateTime. That comparator works across objects of various time zones.
To query a database, convert that pair of DateTime objects into java.sql.Timestamp objects. You do that by extracting and passing the count of milliseconds since the epoch of 1970 in UTC.
long m = today.getMillis();
java.sql.Timestamp tsToday = new java.sql.Timestamp( m );
Title is pretty much the question, when PFObject is created what values are stored in updatedAt and createdAt properties? Are those values from local device time or they are world time and 100% correct.
I need somehow to store a current world date, without letting user to trick my app by switching the date in setting.
Parse.com set it for you in order to let you keep track of the created date and updated date. Time zone is PST so that when you should set PST as your time zone from your time handler object and it will offset it for you automatically.
EDIT: I have this experience before when I was working with my Indian coworkers. The default time zone GMT and it will not work unless Parse.com returns GMT time zone. So, we have to set PST as time zone in our time handler object and it will offset properly.
You should consider those values are set / updated by parse when data hits the server and treat them as almost private as you don't have direct control. If you need to protect some information then you should use cloud code to update it and set appropriate ACLs to prevent access to it from other routes.