tags:

views:

274

answers:

2

Hi ,

i have a time zone in 2010-05-04T05:27:00.000Z format which indicates the GMT time and i want to add GMT 10+ in to it using php.

i can do that thing using following code but how would i directly add 2010-05-04T05:27:00.000Z and GMT 10+ so that i can get a valid date and time.

$offset=10*60*60; 
$dateFormat="d-m-Y H:i::m:s";
echo $timeNdate=gmdate($dateFormat, time()+$offset);
A: 

Use DateTime class http://php.net/manual/en/book.datetime.php exacly DateTime::Add() http://www.php.net/manual/en/datetime.add.php

You have some example here:

<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";

$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>

And another:

<?php
$date = new DateTime('2000-12-31');
$interval = new DateInterval('P1M');

$date->add($interval);
echo $date->format('Y-m-d') . "\n";

$date->add($interval);
echo $date->format('Y-m-d') . "\n";
?>
Svisstack
i am getting error while using add function PHP Fatal error: Call to undefined method DateTime::add() and for second code PHP Fatal error: Class 'DateInterval' not found error.i am using php 5.2.8
hunt
@hunt: In links are you given can read the function DateTime::add() and DateInterval class is availabe from version (PHP 5 >= 5.3.0) then you have too old php for use it.
Svisstack
is there another way for php 5.2.8 ?
hunt
@hunt: You can use this for set datetime zones. http://pl.php.net/manual/en/function.date-timezone-set.php
Svisstack
+1  A: 

Maybe I'm missing the point but are you not really looking for DateTime::setTimezone?

$timezone = new DateTimeZone('Etc/GMT-10'); // GMT+10:00
$datetime = new DateTime('2010-05-04T05:27:00.000Z');
$datetime->setTimezone($timezone);
echo $datetime->format('r');
// Tue, 04 May 2010 15:27:00 +1000
salathe