Jethro Hazelhurst
Jethro Hazelhurst

Reputation: 3285

Displaying GMT in users Time Zone using DateTime and DateTimeZone not working

Background

I am trying to create an events scheduler that shows a days worth of events to a user. It looks like this.

enter image description here

On the left you can see the 15min time intervals, and underneath each interval is the corresponding unix timestamp.

problem:

Take two users for example.

Gemma is from london which is +01:00 GMT

Françoise comes from Paris which is +02:00 GMT

When Gemma looks at her events schedule each 15 minute row should show ...

So take the timeslots from 10:00 AM to 11:00 AM GMT at 2016-9-30 for example, they would look something like this.

For Gemma:

  • 11:00 => 1475229600 (10:00 GMT)
  • 11:15 => 1475230500 (10:15 GMT)
  • 11:30 => 1475231400 (10:30 GMT)
  • 11:45 => 1475232300 (10:45 GMT)
  • 12:00 => 1475233200 (11:00 GMT)

For Françoise:

  • 12:00 => 1475229600 (10:00 GMT)
  • 12:15 => 1475230500 (10:15 GMT)
  • 12:30 => 1475231400 (10:30 GMT)
  • 12:45 => 1475232300 (10:45 GMT)
  • 13:00 => 1475233200 (11:00 GMT)

My Code

Here are my functions which generate the time slots

/**
 * intervals
 */
public function getTimeSlots($year, $month, $day, $start_time = '06:00', $end_time = '20:00')
{
    $date = $year . '-' . $month . '-' . $day; // 2016-9-30
    $unix_base_time = $this->getUnixTimeStamp($date); // unix timestamp for 2016-9-30 00:00

    $seconds_start = strtotime('1970-01-01 ' . $start_time . ' UTC'); // 21600 seconds
    $seconds_end   = strtotime('1970-01-01 ' . $end_time . ' UTC');   // 72000 seconds

    while ($seconds_start <= $seconds_end) {
        $slots[$unix_base_time + $seconds_start] = $this->getTime($date, '+' . $seconds_start . ' seconds');
        $seconds_start = $seconds_start + 900; // plus 15 minutes
    }
    return $slots;
}

/**
 * timestamps
 */
public function getUnixTimeStamp($offset = 'now')
{
    $date = new DateTime($offset, new DateTimeZone('UTC'));
    return $date->format('U'); // returns a unix timestamp
}


/**
 * time
 */
public function getTime($date = 'now', $offset = 'now')
{
    $date = new DateTime($date, new DateTimeZone($this->session->userdata('time_zone')));
    $date->modify($offset);
    return $date->format('H:i');
}

If I var_dump the output I get the following:

array(57) {
  [1475215200]=>
  string(5) "06:00"
  [1475216100]=>
  string(5) "06:15"
  [1475217000]=>
  string(5) "06:30"
  [1475217900]=>
  string(5) "06:45"
  [1475218800]=>
  string(5) "07:00"
  etc......
}

The unix timestamp is correct, but the times are displaying as GMT and not the users time zone!

So obviously I am doing something wrong in the getTime() function.

I can't work out why it is returning the time ('H:i') as GMT when I am clearly injecting the DateTimeZone object with the users timezone passed as an argument.

Upvotes: 0

Views: 504

Answers (1)

somnium
somnium

Reputation: 1547

When creating a new DateTime object the timezone that you pass in is considered the timezone of the passed in date. In your case you are telling DateTime to interpret the seconds once as GMT+1 and once as GMT+2.

Try the following instead:

// our $date is in UTC because we derived it from unix timestamps
$date = new DateTime($date,
       new DateTimeZone('UTC'));
$date->modify($offset);
// update the timezone. The time will be interpreted accordingly.
$date->setTimeZone(new DateTimeZone($this->session>userdata('time_zone')));
return $date->format('H:i');

Upvotes: 1

Related Questions