Background:
I am displaying a list of times as an associative array. The array looks like this (11:00 AM to 12:00 AM):
array(5) {
[1475226000]=>
string(35) "September 30, 2016, 11:00 am +02:00"
[1475226900]=>
string(35) "September 30, 2016, 11:15 am +02:00"
[1475227800]=>
string(35) "September 30, 2016, 11:30 am +02:00"
[1475228700]=>
string(35) "September 30, 2016, 11:45 am +02:00"
[1475229600]=>
string(35) "September 30, 2016, 12:00 pm +02:00"
}
The key is a unix timestamp. The value is the formatted unix timestamp displayed in the users time zone.
My Code
Here is my commented class that generates the array:
<?php
class Time
{
public function __construct()
{
date_default_timezone_set('UTC');
}
public function getTimeSlots($year, $month, $day, $start_time = '11:00', $end_time = '12:00')
{
$date = $year . '-' . $month . '-' . $day;
// get GMT timestamp of 2016-09-30 00:00 Europe/London
$gmt_date = strtotime($this->getRelativeDateTime($date));
$gmt_date = $gmt_date - 7200;
// subtract from or add to $gmt_date whatever our timezone offset in hours is
// get start time offset in seconds from 2016-9-30 00:00
$seconds_start = strtotime('1970-01-01 ' . $start_time . ' UTC');
// get end time offset in seconds from 2016-9-30 00:00
$seconds_end = strtotime('1970-01-01 ' . $end_time . ' UTC');
$unix_seconds_start = $gmt_date + $seconds_start; // GMT
$unix_seconds_end = $gmt_date + $seconds_end;
// echo $unix_seconds_start . date('Y-m-d H:i', $unix_seconds_start);
// echo '<br>';
// echo $unix_seconds_end . date('Y-m-d H:i', $unix_seconds_end);
while ($unix_seconds_start <= $unix_seconds_end) {
$dt = new DateTime('@' . $unix_seconds_start);
$dt->setTimezone(new DateTimeZone('Europe/Paris'));
$slots[$unix_seconds_start] = $dt->format('F j, Y, H:i a P');
$unix_seconds_start = $unix_seconds_start + 900;
}
echo '<pre>', var_dump($slots), '</pre>';
}
public function getRelativeDateTime($date)
{
$date = new DateTime($date, new DateTimeZone('Europe/Paris'));
return $date->format('Y-m-d H:i');
}
}
$time = new Time;
$time->getTimeSlots('2016', '09', '30');
// we want var_dump to show the following
// --------------------------------------
//
// array () {
// from 00:00
// 1234567890 (unix timestamp) => '00:00' (users time)
// 1234567890 (unix timestamp) => '00:15' (users time)
// 1234567890 (unix timestamp) => '00:30' (users time)
// 1234567890 (unix timestamp) => '00:45' (users time)
// to 24:00
// }
Issue
I want my times to start strictly at 00:00 AM and end at 24:00pm for the user, however as you can see if you run the code I am getting an offset depending on my users time offset.
That means if the user has his time zone as Europe/London +01:00 my array starts at 01:00.
The problem is on line 16. As you can see if you uncomment line 16 and run the code it works but only because I am explicitly subtracting two hours (in seconds) from the timestamp.
http://sandbox.onlinephpfunctions.com/code/d19b6fc5335f41af491dfedcfae2c390aa3000ec
Question
Is there a way using DateTime (or any other method!) to subtract the users time zone offset from the $gmt_date variable?