Parveez Ahmed
Parveez Ahmed

Reputation: 1417

displaying unix timestamp-time not matching to server time

I have used in PHP to print the current date and time:

              $now = time();
              echo date('j M, Y H:i:s',$now); 

The result is accurate as far as date,month,year is concerned. But the time is not matching on the same computer(as am testing in localhost).PHP script shows: 9 Nov, 2013 02:35:11 whereas the time on my pc is: 8:05 AM

Upvotes: 1

Views: 1766

Answers (3)

Henrique Barcelos
Henrique Barcelos

Reputation: 7900

There are three ways of working with timezones:

  1. Setting a default timezone in your php.ini file (NOT recommended):

    [Date]
    ; Defines the default timezone used by the date functions
    ; http://php.net/date.timezone
    date.timezone = America/Sao_Paulo
    

    This will work for just PHP files running on the server that is configured by this php.ini.

  2. Setting a default timezone in you initialization PHP script:

    date_deafult_timezone_set

    date_default_timezone_set('America/Sao_Paulo');
    

    This will work for almost all scripts.

  3. Working with a DateTimeZone object:

    $date = new DateTime('2013-11-09 01:08:32', new DateTimeZone('America/Sao_Paulo'));
    

    This will work for all scripts, but can get a little bit tedious.

Upvotes: 1

wazy
wazy

Reputation: 1065

Maybe this will be use to you (change to your timezone of course):

date_default_timezone_set("America/Phoenix");

Conversion function:

// converts from UTC to Phoenix time
function timezone_convert($datetime) {
    $datetime = new DateTime($datetime);
    $phx_time = new DateTimeZone('America/Phoenix');
    $datetime->setTimezone($phx_time);
    $datetime = $datetime->format('Y-m-d H:i:s');   
    return $datetime;
}

Upvotes: 0

Guillermo Mansilla
Guillermo Mansilla

Reputation: 3889

The reason is that your php.ini is using a different timezone.

Upvotes: 0

Related Questions