1

I'm trying to convert time stamp received from youtube api video feed. the received time stamp is 2013-01-11T06:45:52.000Z. I think it is GMT time stamp(correct me if I'm wrong). I wrote a function to convert the time stamp to IST.

function formatTime($ydatetime) {
    $timestamp = explode("T", $ydatetime);
    $date = $timestamp[0];
    $gmtime = substr($timestamp[1], 0, 8);
    $gmtimestamp = $date . " " . $gmtime;
    $datetime = new DateTime($gmtimestamp, new DateTimeZone('GMT'));
    $datetime->setTimezone(new DateTimeZone('IST'));
    return $datetime->format('Y-m-d H:i:s');
}

But it returns the time stamp as 2013-01-11 08:45:52 where the difference is 2 Hrs only. The actual difference between GMT and IST is 5.30 Hrs. please somebody help me.

Krishna Raj
  • 846
  • 1
  • 12
  • 33
  • take a look at this http://stackoverflow.com/questions/7811609/php-add-two-hours-to-date-variable – Color Jan 11 '13 at 09:41

5 Answers5

4

Did you tried time zone "Asia/Calcutta" or

any one in http://php.net/manual/en/timezones.indian.php

Vishnudev
  • 94
  • 2
  • 2
    thanks for that. I got the timezone as `IST` from [this stackoverflow link](http://stackoverflow.com/questions/13216185/php-converting-gmt-to-ist/13216191#13216191). now its changed to "Asia/Calcutta" and got working... – Krishna Raj Jan 11 '13 at 09:47
1

Try

function formatTime($ydatetime) {
   date_default_timezone_set('Asia/Kolkata'); //<--This will set the timezone to IST
   $str = strtotime($ydatetime);
   return date('Y-m-d H:i:s', $str);
}
asprin
  • 9,579
  • 12
  • 66
  • 119
  • 1
    that's really bad idea. it returned `1970-01-01 05:30:00` – Krishna Raj Jan 11 '13 at 09:44
  • What was the value of `$ydatetime`? – asprin Jan 11 '13 at 09:48
  • you must read the question before answering. I already explained that its the time returned from youtube video feed which is GMT. – Krishna Raj Jan 11 '13 at 09:51
  • 1
    I meant did you pass that value as the argument? I've a feeling that `$ydatetime` was empty and hence it returned the date that you mentioned above. – asprin Jan 11 '13 at 10:04
  • huh? why should I pass an empty variable to a function? I echo'd the variable just before I use it. It was not empty. – Krishna Raj Jan 11 '13 at 12:15
  • 1
    You certainly must have missed something because the output is showing up [**Demo**](http://codepad.viper-7.com/pTld6F) – asprin Jan 11 '13 at 12:55
  • may be.. anyway I got the solution and thanks for trying to help me.. and please edit the answer so that I can undo the down vote. thanks.. – Krishna Raj Jan 12 '13 at 10:26
  • 1
    I didn't change anything in the answer. Whatever is in my answer is the same as in the demo used. – asprin Jan 12 '13 at 13:32
  • 1
    Why would I edit the answer when there is nothing wrong in it? You know what, forget it. Let's not push this anymore. – asprin Jan 16 '13 at 07:56
  • to undo my down vote to your answer. If you don't need it, then just leave it. – Krishna Raj Jan 17 '13 at 17:06
1

I was facing the same issue, wanted to convert GMT to IST. This one worked for me :

$date = new DateTime('2018-04-08T14:30:00.000Z', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $date->format('Y-m-d H:i:s');

Just wanted to share it if someone has the same issue in future :)

0

try

echo date('Y-m-d H:i:s',strtotime('+330 minutes', 0));  

or

date_default_timezone_set('Asia/Kolkata');
  echo date('Y-m-d H:i:s');  

first get time by

$time = new DateTime('now', new DateTimeZone('UTC'));
    // then convert it to IST by
    $time->setTimezone(new DateTimeZone('IST'));
asprin
  • 9,579
  • 12
  • 66
  • 119
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
  • you copied the answer from [here](http://stackoverflow.com/questions/13216185/php-converting-gmt-to-ist/13216191#13216191) right? I have already checked that. thnaks – Krishna Raj Jan 11 '13 at 09:50
0

I recently found that PHP doesn’t have an inbuilt function for conversion of time/date between multiple time zones and neither could I find a third-party function for the purpose. However, I did find the PEAR class which has inbuilt support for multiple time-zones but it cannot be used by “including” itself in the php page. PEAR class can be used only after installation, which may not be feasible in each and every case. To avoid the hassles of installation I have written a set of 3 functions in PHP which wil allows you to –

  1. Convert GMT to local time zone
  2. Convert local time zone to GMT
  3. Convert between two different time zones.

    /**
     *Convert the time in GMT timestamp into  user's local time zone timestamp
     * @param time $gmttime
     * @param string $timezoneRequired
     * $gmttime should be in timestamp format like '02-06-2009 09:48:00.000'
     * $timezoneRequired sholud be a string like 'Asia/Calcutta' not 'IST' or 'America/Chicago' not 'CST'
     * return timestamp format like '02-06-2009 09:48:00' (m-d-Y H:i:s) Can also change this format
     * $timestamp = $date->format("m-d-Y H:i:s"); decide the return format                      
    */
    
    
    function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
    {
        $system_timezone = date_default_timezone_get();
    
        date_default_timezone_set("GMT");
        $gmt = date("Y-m-d h:i:s A");
    
        $local_timezone = $timezoneRequired;
        date_default_timezone_set($local_timezone);
        $local = date("Y-m-d h:i:s A");
    
        date_default_timezone_set($system_timezone);
        $diff = (strtotime($local) - strtotime($gmt));
    
        $date = new DateTime($gmttime);
        $date->modify("+$diff seconds");
        $timestamp = $date->format("m-d-Y H:i:s");
        return $timestamp;
    }
    
    /**
    *Use: ConvertGMTToLocalTimezone('2009-02-05 11:54:00.000','Asia/Calcutta');
    *Output: 02-05-2009 17:24:00 || IST = GMT+5.5
    */
     /**
     *Convert the time in user's local time zone timestamp into GMT timestamp  
     * @param time $gmttime
     * @param string $timezoneRequired
     * $gmttime should be in timestamp format like '02-06-2009 09:48:00.000'
     * $timezoneRequired sholud be a string like 'Asia/Calcutta' not 'IST' or 'America/Chicago' not 'CST'
     * return timestamp format like '02-06-2009 09:48:00' (m-d-Y H:i:s) Can also change this format
     * $timestamp = $date->format("m-d-Y H:i:s"); decide the return format Date:06/02/2009
    */
    
    function ConvertLocalTimezoneToGMT($gmttime,$timezoneRequired)
    {
        $system_timezone = date_default_timezone_get();
    
        $local_timezone = $timezoneRequired;
        date_default_timezone_set($local_timezone);
        $local = date("Y-m-d h:i:s A");
    
        date_default_timezone_set("GMT");
        $gmt = date("Y-m-d h:i:s A");
    
        date_default_timezone_set($system_timezone);
        $diff = (strtotime($gmt) - strtotime($local));
    
        $date = new DateTime($gmttime);
        $date->modify("+$diff seconds");
        $timestamp = $date->format("m-d-Y H:i:s");
        return $timestamp;
    }
    
    /**
    *Use: ConvertLocalTimezoneToGMT('2009-02-05 17:24:00.000','Asia/Calcutta');
    *Output: 02-05-2009 11:54:00 ||  GMT = IST-5.5
    */
    
    
    
    
    /**
     *Convert the time in user's local time zone timestamp into another time zone timestamp
     * @param time $gmttime
     * @param string $timezoneRequired
     * $gmttime should be in timestamp format like '02-06-2009 09:48:00.000'
     * $timezoneRequired sholud be a string like 'Asia/Calcutta' not 'IST' or 'America/Chicago' not 'CST'
     * return timestamp format like '02-06-2009 09:48:00' (m-d-Y H:i:s) Can also change this format
     * $timestamp = $date->format("m-d-Y H:i:s"); decide the return format Date:06/02/2009.
    */
    
    
    function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired)
    {
        $system_timezone = date_default_timezone_get();
        $local_timezone = $currentTimezone;
        date_default_timezone_set($local_timezone);
        $local = date("Y-m-d h:i:s A");
    
        date_default_timezone_set("GMT");
        $gmt = date("Y-m-d h:i:s A");
    
        $require_timezone = $timezoneRequired;
        date_default_timezone_set($require_timezone);
        $required = date("Y-m-d h:i:s A");
    
        date_default_timezone_set($system_timezone);
    
        $diff1 = (strtotime($gmt) - strtotime($local));
        $diff2 = (strtotime($required) - strtotime($gmt));
    
        $date = new DateTime($time);
        $date->modify("+$diff1 seconds");
        $date->modify("+$diff2 seconds");
        $timestamp = $date->format("m-d-Y H:i:s");
        return $timestamp;
    }
    
    
    
    /**
    *Use: ConvertLocalTimezoneToGMT('2009-02-05 17:24:00.000','Asia/Calcutta','America/Chicago');
    *Output: 02-05-2009 05:54:00 ||  IST = GMT+5.5, CST = GMT-6 || IST - CST = 11.5
    */
    

    find blog here

Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43