The diffrence between 2 dates - current time and mysql time. This can be backdoored when user change his PC time and the code is Bypassing him from the diffrence check.
I've tried to use
mktime()
but its not working when my mysql date is 1451094007 (Sat, 26 Dec 2015 01:40:07 GMT) and real world time is for example 1451108407 (Sat, 26 Dec 2015 05:40:07 GMT) 4 hours later, and minimum difference is 10 hours user can still add some hours on him own PC and bypass time.
How can I get any world time which can't be manipulated?
You get all timezones using in the world store in array then run in loop
foreach($timezonearray as $timezone){
$time = new DateTime($timezone);
echo $time->format('Y-m-d H:i:s');
}
it will print result like this
http://www.timehubzone.com/worldclock
single way
$time = new DateTime('Africa/Abidjan');
echo $time->format('Y-m-d H:i:s');
you can get all time zones here
timezones list
You can use other services such as http://worldclockapi.com/
(see http://worldclockapi.com/api/json/utc/now)
Fetch the result and feed them to variable.
Basically, your application should use the server time where it's hosted (configured with server clock) and time() should have respected the server clock
Related
Hello stackoverflow community.
I develop a web app and the concept is to display historical currency exchange rates based on time series from past.
For example a user may request exchange rates from 22 May 2020 13:00 to 26 MAY 2020 22:00. Then my backend run a loop and get the rates between those two date times each hour.
All rates in database stored in GMT time zone.
And here is the problem. Let's suppose a user make a request from a time zone offset +10:00. So if this user pick as last date time 26 MAY 2020 22:00, I guess I should grab from my database the rate in 26 MAY 2020 12:00, so to subtract 10 hours.
May this sound stupid, but I'm stuck with this.
What is my logic:
a) Get the users time zone offset via javascript in front-end
var get_timezone_offset = new Date().getTimezoneOffset();
var hrs = parseInt(-(timezone_offset / 60));
var mins = Math.abs(timezone_offset % 60);
var timezone_offset = hrs + ':' + mins;
b) Send users time zone offset to my backend
c) Get rates from my database and convert date stored from GMT to users time zone offset via PHP's Datetime object
$date = new \DateTime('2020-05-26 22:00');
$date->modify(-10 hours);
$date->format('Y-m-d H:i:s');
Is this right? I won't display wrong rates to my users.
Thank you in advance
Please read the timezone tag wiki, especially the section titled "Time Zone != Offset". In short, you cannot assume the offset taken now from the user is the same one that will apply at any other point in time.
A simple example is for time zones like America/New_York, which presently is UTC-4, but will switch to UTC-5 when daylight saving time ends in November. But besides DST, many time zones have had changes in their standard time offsets.
Instead of getting the current numeric offset, get the IANA time zone identifier from the browser.
const tzid = Intl.DateTimeFormat().resolvedOptions().timeZone;
// example: "America/New_York", "Asia/Kolkata", "Europe/Athens", etc.
Then you can use this identifier with PHP's built-in time zone support. For example, the following converts from a local time in a given time zone to the equivalent time in UTC:
$tz = new DateTimeZone("America/New_York");
$date = new DateTime('2020-05-26 22:00', $tz);
$date->setTimeZone(new DateTimeZone("UTC"));
echo $date->format('Y-m-d H:i:s');
I am building backend queue system. My app's users need to automatically fetch data from server around 08:00:00 AM, individually for each time zone.
Every user needs to be assigned to a specific time a day. He can fetch data only at this time as the app uses API that has specific calls-per-minute limits.
How do I synchronize clients with server?
NOTE
I ran into specific problems, and solved it already. I am posting the solution right away as a complete answer that combines many answers I found on SO while solving it.
Core of the solution
For clarity use time values in UTC that is supported in each Java/PHP/MySQL, because:
Although GMT and UTC share the same current time in practice, there is a basic difference between the two:
GMT is a time zone officially used in some European and African countries. The time can be displayed using both the 24-hour format (0 - 24) or the 12-hour format (1 - 12 am/pm).
UTC is not a time zone, but a time standard that is the basis for civil time and time zones worldwide. This means that no country or territory officially uses UTC as a local time.
source
It gives you simple solution as once you use UTC, you only need to convert it to server's or clients' time zone for display purposes.
Managing client's time zone
You need to send client's time zone to backend to calculate what time do you want him to call API. You want to convert 08:00:00 local time to UTC, but here's a trick, because there are incompatible time zones' strings between Java and PHP.
// Java/Android
SimpleDateFormat sdf = new SimpleDateFormat("z");
I live in Poland, and using the code above I get 2 different values depending on seasons (CET for winter time and CEST for summer time).
// PHP
$tz1 = new DateTimeZone('CET');
$tz2 = new DateTimeZone('CEST');
The problem is that when I pass it to PHP, CET works perfectly as it's supported time zone string, but CEST is not.
To unify your code, you need to use:
// Java/Android
SimpleDateFormat sdf = new SimpleDateFormat("ZZZZ");
which gives you a time zone likethis:
GMT+01:00 // for CET
GMT+02:00 // for CEST
Remember that when you send it in URL like http://api.domain.com?timezone=GTM+02:00, you need to change + into %2B as timezone converted to GTM 02:00 won't work in PHP.
Calculating queue time for users
Once you get client's time zone, in PHP you convert 08:00:00 AM local time to UTC.
$tz = new DateTimeZone('GMT+02:00');
$dt = new DateTime('2017-03-30 08:00:00', $tz);
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('H:i:s');
// echoes 06:00:00
Then you store calculated value in MySQL at type TIME column. You don't need to care about time zone in the database as TIME and DATE types are time zone independent.
Setting alarm at calculated UTC time
You get 06:00:00 as a response in the app, and you set AlarmManager using Calendar object like this:
// set UTC as a time zone
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(new Date());
long timeNow = cal.getTimeInMillis();
// set 06:00:00
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.HOUR_OF_DAY, 6);
// make sure to set alarm in future
long timeAlarm = cal.getTimeInMillis();
if (timeAlarm <= timeNow) {
cal.add(Calendar.HOUR_OF_DAY, 24);
}
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),
24*60*60*1000, pintent);
In my application I'm trying to calculate the time left until midnight GMT (UK time). At the moment I'm doing this:
$now = new DateTime();
$timeToMidnight = $now->setTimezone(new DateTimeZone('Europe/London'))->diff(new DateTime('tomorrow'))->format('%h hours, %i minutes and %s seconds');
The code is working, however it seems to be one hour behind (using GMT -1). At the moment the time is 11:49PM and the output is this:
1 hours, 10 minutes and 36 seconds
I've double checked my php.ini and I also have that timezone set as GMT:
date.timezone = Europe/London
This is also confirmed by checking phpinfo().
What gives? Why isn't my application using the correct timezone?
I tested this on Linux PHP 5.5.5, with Europe/London set as the timezone in php.ini. I actually set the clock back four hours to do this, too. The minimal code I used to reproduce was:
$d = new DateTime('tomorrow');
echo $d->format('c e');
The (correct) output was:
2013-10-27T00:00:00+01:00 Europe/London
I am going to look for a bug in PHP or a bug in the timezone data. To find out which, we'll see what some other program makes of midnight in London tonight. Epoch Converter tells me this should have a Unix timestamp of 1382828400. To double check that timestamp, I ran in PHP:
$d = new DateTime('27-10-2013');
echo $d->format('U');
It also returned 1382828400. So, let's see what it's supposed to show...
TZ=Europe/London date --date="#1382828400" +%c
The output was:
Sun 27 Oct 2013 12:00:00 AM BST
Correct! So tzdata is fine. So let's look at PHP.
I ran your sample code, along with the date command, and got the following output:
1 hours, 29 minutes and 53 seconds
Sat Oct 26 21:30:07 UTC 2013
Sat Oct 26 22:30:07 BST 2013
This is, of course, correct.
I think at this point we have ruled out bugs in both tzdata and PHP, and need to look at configuration issues and programmer expectations.
First, as I noted previously, Europe/London is not UTC, which has no concept of summer time and thus does not change twice per year. Since it doesn't cause such problems, it's a best practice for servers to run on UTC, regardless of what time zone their users are in, and a best practice for programs to use UTC internally and then convert to/from local time zones for display and user input only.
My best guess is that your server's running PHP is actually set to use UTC and not Europe/London as its default time zone. This is the only configuration in which I could reproduce your issue. The results of that test were:
date.timezone = UTC
2 hours, 24 minutes and 36 seconds
Sat Oct 26 21:35:24 UTC 2013
Sat Oct 26 22:35:24 BST 2013
Going forward, you should work in UTC (and with Unix timestamps) wherever practical, and convert to local time as early in processing user input, and as late in displaying it, as you can. An edge case like this one, where summer time is about to end, may be an exception, but you have to be extra careful to ensure that each new DateTime object you construct has the correct time zone set when you construct it, and also be aware that they will have issues like this.
See also the huge and informative Daylight saving time and time zone best practices
Finally, to "fix" your code, let's do this:
$tz = new DateTimeZone('Europe/London');
$now = new DateTime('now', $tz);
$midnight = new DateTime('tomorrow', $tz);
$timeToMidnight = $now->diff($midnight);
echo $timeToMidnight->format('%h hours, %i minutes and %s seconds');
I have a web application that allows users to select to have a certain operation performed at a specific hour on a specific weekday (ex: 7pm Friday) -- recurring every weekday. My users are located in different timezones. Therefore I would like to store the day/hour in one time zone format so that my cron job which runs hourly can query my database and quickly grab all the rows that represent tasks to be performed that hour. What's the best way to accomplish this?
Basically I need to know how to take
Friday 7:00pm in Boston, MA or Saturday 3:00pm San Francisco, CA and convert both to GMT (and store in my DB). That way when I run my cron job once an hour I know exactly what value to query the database for given what GMT day/hour it currently is.
EDIT 1
Through playing around I came up with something:
date_default_timezone_set('America/New_York');
$time = strtotime('Friday 8:00pm');
echo date('w : G', $time);
echo '<br>';
date_default_timezone_set('GMT');
$date = date('w : G', $time);
echo $time;
echo '<br>';
echo $date;
The output is:
5 : 20
1331942400
6 : 0
Is this basically the right track?
EDIT 2
Useful detail, I'm actually lucky enough to have my user's timezone information stored as one of these:
http://www.php.net/manual/en/timezones.php
If you store the users time zone already, you can add the GMT offset to the time to get the GMT date.
I'm located in Colorado which is currently -7 GMT:
$offset = -7;
$gmt = ($offset * 60 * 60) + time();
would give the the unix timestamp for the GMT timezone.
Then make sure your server has GMT as the default or set timezone, you can set the timezone using:
date_default_timezone_set('GMT');
OK. So I have been trying to implement a timer. Now a very weird thing is happening and I can't understand why ?.
Basically I am trying to find the difference between the last access and the current time. I am storing the time of last access in the database. This value is according to the server time. But when I try the time() function of php it shows me values which are 5-6 hours behind the time that I have in the database.
For example: here is my code :
$t1= strtotime($played_row->timer); // Time from the database with CURRENT_TIMESTAMP
$t2= strtotime("now"); // Get the current time
It shows Year: 2012 Month: 01 Day: 21 - 05:28 pm for t2
and Year: 2012 Month: 01 Day: 21 - 10:28 pm for my timestamp values.
Can anyone tell my why is that ?
P.S: I am running the code on my computer itself.
At a guess I would say that your database and PHP are using two different timezone offsets.
Most likely this is a timezone issue: if you are in the Eastern timezone, you are 5 hours away from UTC right now. If one sytem is returning local time and another is returning UTC this is what you will see.
Try using date_default_timezone_set() to set the timezone in PHP that is used in your database.
date_default_timezone_set — Sets the default timezone used by all
date/time functions in a script
Alse see date_default_timezone_get() how to get ini-set timezone.