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');
Related
I run a business and have been developing a CMS / invoicing on my website.
"Appointment"
12:30 PM Thu Aug 5 2021
Though I can format this any way I like, is it possible at all to get the unix time stamp of that?
The purpose of doing so would be so I can just insert the unix timestamp and be able to sort my appointments by the closest to date.
Thank you.
$timestamp = strtotime('1/1/2021 21:00');
Replace 1/1/2021 21:00 to the desired date.
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');
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
I am trying to rotate phone numbers depending on the time of day. If the time is between 8am and 5pm, it should show the 888 number. If it is after 5pm but before 8am it should show the 777 number. This is what I got thus far, but it only shows the 777 number as my output, even though the time is between the hours of 8am and 5pm.
$dayPhoneNumber = "1-888-888-8888";
$nightPhoneNumber = "1-777-777-7777";
$currentPhoneNumber = "";
$nowHour = date("H");
$startHour = 8;
$endHour = 17;
if ($nowHour >= $startHour && $nowHour <= $endHour){
echo $dayPhoneNumber;
} else {
echo $nightPhoneNumber;
}
My guess is your server's time is not the same as your computer's time. If you're using a hosting solution, your server may very well be across the country or on a different continent, meaning the time will be different.
If you just need to worry about one location and not where the end user is, then this should be easy to do. If you're on the West Coast of the US
date_default_timezone_set("America/Los_Angeles");
$hour = date("H", time());
Here is a list of Time Zones: http://www.php.net/manual/en/timezones.php
If you need to know the end user's time zone when determining the hour, here's a discussion on doing that: How to get client's timezone?
If you're dealing with time zone issues, you may also want to retrieve the time using UTC, and then add or subtract whatever number of hours to get to the correct time zone: get UTC time in PHP
When a user signs up for my site they enter their weekly availability in this format:
Monday 1300 to 1400
Monday 2100 to 2200
Tuesday 1200 to 1300
Tuesday 1400 to 1500
Etc.
They also indicate their timezone based on a select menu, such as America/Los_Angeles.
What I want to happen is for those times to be adjusted based on the timezones of users that visit their profiles who have different timezones set. What is the best solution for this?
Store their availability as seconds from the EPOCH, and then convert those to proper times for users based on their time zone.
Store the difference between UTC and selected timezone in value of field
<option value="3">UTC+3 timezone</option>
<option value="-5">UTC-5 timezone</option>
then add or cut it from to the output of hours.
If such difference provide negative result or more than 24 hours - set next or previous day of the week.
I think you don't realy need unixtime for such intervals.
I actually ended up doing this:
<?php while ($ava = mysql_fetch_array($avail_action)) {
date_default_timezone_set($profile_owners_timezone);
$timestamp_from = strtotime($ava['time_from']);
$timestamp_to = strtotime($ava['time_to']);
date_default_timezone_set($_SESSION['timezone']);
$time_from = date("g:i A", $timestamp_from);
$time_to = date("g:i A", $timestamp_to); ?>
Basically I got the profile "owner's" timezone that he/she entered, set timezone to that, then set the timezone to "viewer's" timezone and set the variable to that.
Thanks all for the help!