I’m having some problems with showing how much time is left.
This is my code:
echo date( "H:i:s", $show_job['deadline'] - time() );
My server timezone is: Europe/Amsterdam
This is what the time going into the database is:
$currentTimestamp = time();
$in24hourstime = strtotime("+24 hours", $currentTimestamp);
The problem is that some of my users are getting 10 hours rather than 24 hours, for example. My guess is that these users are from different time zones. I am in London.
What could be causing this error? Thanks in advanced.
First its a good idea to explicitly set the default timezone in your script using the PHP date function like so:
See http://php.net/manual/en/function.date-default-timezone-set.php
This ensures all date calculations use the correct internal timezone. When displaying the date back to the user you can adjust the time to their timezone accordingly.
Modifying your example slightly lets create two variables with UNIX timestamps for now and in 24 hours:
To calculate the difference we can subtract one from the other to give us the difference in seconds:
In our example the value of $remaining is 86400 seconds. Had this value been 0 or a negative number we can assume the deadline has passed.
As an aside you may find using PHP’s DateTime class useful as in my opinion it provides many benefits over the inbuilt time() and date() and an easier way to deal with timezones:
See: http://php.net/manual/en/book.datetime.php