Skip to main content

How do i get the gmt time in php



I have a server which is set to EST and all records in the db are set to EST. i would like to know how to set it to gmt? I want to put a time zone option to my users.





Source: Tips4all

Comments

  1. No matter the server is in which GMT time zone here is extremely easy way to get time and date for any time zone. This is done with time() and gmdate() function. gmdate() function normally give us GMT time but by doing a trick with time() function we can get GMT+N or GMT-N means we can get time for any GMT time zone.

    For example you have to get time for GMT+5 we will do it like following

    <?php
    $offset=5*60*60; //converting 5 hours to seconds.
    $dateFormat="d-m-Y H:i";
    $timeNdate=gmdate($dateFormat, time()+$offset);
    ?>


    Now if you have to get the time which is GMT-5 now we will just subtract the offset from the time() instead of adding into time like in following example we are getting time for GMT-4

    <?php
    $offset=4*60*60; //converting 5 hours to seconds.
    $dateFormat="d-m-Y H:i";
    $timeNdate=gmdate($dateFormat, time()-$offset);
    ?>

    ReplyDelete
  2. I would strongly suggest avoiding messing with UNIX timestamps to make it look like a different time zone. This is a lesson I've learnt the hard way, way too many times.

    A timestamp is the number of seconds since Midnight 1 January 1970, GMT. It doesn't matter where you are in the world, a given timestamp represents the exact same moment in time, regardless of time zones. Yes, the timestamp "0" means 10am 1/1/70 in Australia, Midnight 1/1/70 in London and 5pm 31/12/69 in LA, but this doesn't mean you can simply add or subtract values and guarantee accuracy.

    The golden example which stuffed me up every year for way too long was when daylight savings would crop up. Daylight savings means that there are "clock times" which don't exist in certain parts of the world. For example, in most parts of the US, there was no such time as 2:01am on April 2, 2006.

    Enough quasi-intelligible ranting though. My advice is to store your dates in the database as timestamps, and then...


    If you need the local time at the server, use date()
    If you need to get GMT, then use gmdate()
    If you need to get a different timezone, use date_default_timezone_set() and then date()


    For example,

    $timestamp = time();

    echo "BRISBANE: " . date('r', $timestamp) . "\n";
    echo " UTC: " . gmdate('r', $timestamp) . "\n";
    date_default_timezone_set('Africa/Johannesburg');
    echo " JOBURG: " . date('r', $timestamp) . "\n";

    // BRISBANE: Tue, 12 May 2009 18:28:20 +1000
    // UTC: Tue, 12 May 2009 08:28:20 +0000
    // JOBURG: Tue, 12 May 2009 10:28:20 +0200


    This will keep your values clean (you don't have to be worrying about adding offsets, and then subtracting before saving), it will respect all Daylight Savings rules, and you also don't have to even look up the timezone of the place you want.

    ReplyDelete
  3. Posted by General Cucombre

    in Yahoo answers:

    $today_gmt_is = date("l, F j, Y, g:i a", time() - date("Z")) ;




    date("Z") gives you offset of your local time from GMT in seconds. subtract that value from local time to convert local time to GMT time.

    * 1 year ago


    also:
    php.net

    ReplyDelete
  4. Here is a list of the time zones with different from GMT, hope this is helpful!

    echo '<pre>';
    $zones_array = array();
    $timestamp = time();
    foreach (timezone_identifiers_list() as $key => $zone) {
    date_default_timezone_set($zone);
    $zones_array[$key]['zone'] = $zone;
    $zones_array[$key]['diff_from_GMT'] = date('P', $timestamp);
    }
    print_r($zones_array);


    Thanks!

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.