0

I want to create ics start and end time (UTC) with format like:

DTSTART:20141107T110000Z
DTEND:20141107T120000Z

I have following Object as input:

"event": {
    "timezone": "GMT+2",
    "start_time": "2014-11-07 13:00:00", // client local time
    "duration": 30
     },

 function _calc_end_date($start, $duration) {
    $start_date = new DateTime($start, new DateTimeZone('UTC'));
    $end_date = $start_date->add(new DateInterval('PT'.$duration.'M'));
    $end_date_str = $end_date->format('Y-m-d H:i:s');
    return $end_date_str;
 }

 private function dateToCal($timestamp) {
    return GMDATE("Ymd\THis\Z", $timestamp);
}

 $timezone  = $event['timezone']; // ????       
 $start = strtotime($event['start_time'] . " UTC");
 $end_date = $this->_calc_end_date($event['start_time'], $event['duration']);
 $end = strtotime($end_date . " UTC");

So I write:

"DTSTART:" . $this->dateToCal( $start ) . $eol .
"DTEND:" . $this->dateToCal( $end ) . $eol .

This code works but I get result with 2 hours difference. As you can notice I don't entered TimeZone because I don't know how.

$timezone  = $event['timezone']; // 

Somehow I need parse "GMT+2" and calculate

$start = strtotime($event['start_time'] . " UTC");

with TZ.

Please help,

4
  • if you are using UTC why do you need a timezone? UTC is the same all over the world Commented Nov 5, 2014 at 20:47
  • Client iOS sends his local time + his TZ to let me calculate UTC properly. Commented Nov 5, 2014 at 20:48
  • so you have the UTC and you want to calculate the local time? Commented Nov 5, 2014 at 20:50
  • I need convert his local time + TZ to UTC format (timestamp) Commented Nov 5, 2014 at 20:51

3 Answers 3

1
$date = DateTime::createFromFormat( "Y-m-d H:i:s O", $event['start_time'] . ' ' . $event['timezone'] );
$date->setTimezone( new DateTimeZone( 'GMT' ) );
$startDate = $date->format( "Ymd\THis\Z" );

$date->add( new DateInterval( 'PT' . $event['duration'] . 'M' ) );
$endDate = $date->format( "Ymd\THis\Z" );
Sign up to request clarification or add additional context in comments.

Comments

0

Use substring and indexof to grab everything after the "+" and add that number of hours to the time

Comments

0

What is source timezone name? This code works for me:

date_default_timezone_set('UTC');
echo date('Y-m-d H:i', strtotime("2014-11-07 13:00:00 Europe/Warsaw"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.