We help IT Professionals succeed at work.

Php localtime function

Pedro Chagas
Pedro Chagas asked
on
Hi E's, I try to use the localtime function, and I get this example from php.net for I made a test:
 
<?php 
$localtime = localtime(); 
$localtime_assoc = localtime(time(), true); 
echo "<pre>"; 
print_r($localtime); 
echo "</pre>"; 
echo "<pre>"; 
print_r($localtime_assoc); 
echo "</pre>"; 
?>

Open in new window


My doubts is if localtime work with server hour or if work and give me the exact hour where I play the script.
I'm in Portugal, and the script output give me the right hour.
I want to see if work in other parts of the world. Can you give me your locations and the output please?

The best regards, JC
Comment
Watch Question

Marco GasiFreelancer
Top Expert 2010
Commented:
I'm in Italy and output is

Array
(
    [0] => 49
    [1] => 0
    [2] => 13
    [3] => 7
    [4] => 11
    [5] => 111
    [6] => 3
    [7] => 340
    [8] => 0
)

Array
(
    [tm_sec] => 49
    [tm_min] => 0
    [tm_hour] => 13
    [tm_mday] => 7
    [tm_mon] => 11
    [tm_year] => 111
    [tm_wday] => 3
    [tm_yday] => 340
    [tm_isdst] => 0
)

 
Kim WalkerWeb Programmer/Technician
Commented:
This function returns the server's local time, not the client's. It will return US Mountain time for me which is where my hosting server is located instead of US Eastern time where I am located.
Most Valuable Expert 2011
Top Expert 2016
Commented:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

In that article you can learn about two useful things.  Date_default_timezone_set() is a PHP function that may be helpful to you.  See also Practical Application #8.

HTH, ~Ray
Most Valuable Expert 2011
Top Expert 2016
Commented:
My server is in Chicago.  I ran the script at 8:18 am Eastern Standard Time On Wednesday December 7, 2011

Warning: localtime() [function.localtime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CST/-6.0/no DST' instead in /home/websitet/public_html/RAY_temp_joao.php on line 4

Warning: localtime() [function.localtime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CST/-6.0/no DST' instead in /home/websitet/public_html/RAY_temp_joao.php on line 5

Array
(
    [0] => 3
    [1] => 18
    [2] => 7
    [3] => 7
    [4] => 11
    [5] => 111
    [6] => 3
    [7] => 340
    [8] => 0
)

Array
(
    [tm_sec] => 3
    [tm_min] => 18
    [tm_hour] => 7
    [tm_mday] => 7
    [tm_mon] => 11
    [tm_year] => 111
    [tm_wday] => 3
    [tm_yday] => 340
    [tm_isdst] => 0
)

<?php  // RAY_temp_joao.php
error_reporting(E_ALL);

$localtime = localtime();
$localtime_assoc = localtime(time(), true);
echo "<pre>";
print_r($localtime);
echo "</pre>";
echo "<pre>";
print_r($localtime_assoc);

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
Check the output of this variant on your script.
http://www.laprbass.com/RAY_temp_joao.php
<?php  // RAY_temp_joao.php
error_reporting(E_ALL);
echo "<pre>";

// PARIS
date_default_timezone_set('Europe/Paris');
echo date_default_timezone_get();
echo PHP_EOL;
print_r( localtime( time(), TRUE ) );

// SYDNEY
date_default_timezone_set('Australia/Sydney');
echo date_default_timezone_get();
echo PHP_EOL;
print_r( localtime( time(), TRUE ) );

// US EASTERN
date_default_timezone_set('America/New_York');
echo date_default_timezone_get();
echo PHP_EOL;
print_r( localtime( time(), TRUE ) );

Open in new window

I'm in US.  CST.

<pre>Array
(
    [0] => 9
    [1] => 41
    [2] => 17
    [3] => 7
    [4] => 11
    [5] => 111
    [6] => 3
    [7] => 340
    [8] => 0
)
</pre><pre>Array
(
    [tm_sec] => 9
    [tm_min] => 41
    [tm_hour] => 17
    [tm_mday] => 7
    [tm_mon] => 11
    [tm_year] => 111
    [tm_wday] => 3
    [tm_yday] => 340
    [tm_isdst] => 0
)
</pre>