Page 1 of 1

Adjust server time in PHP

Posted: Wed Jun 13, 2007 3:49 pm
by blue-sky
Adjust server time in PHP


in PHP, we can display a date by using date() function..

eg. date("H:i.s");

now, what if for example, your locale is in singapore, and your service is specially for singaporeans (or philippines for filipinos), and your server is in US (hosting)? when you display a date using date() function, it will show the time in US or the time your hosting was using right?


solution: singapore and philippines has the same time zone right? it is GMT+8.. then you can rewrite: date("H:i.s",time()+8);

8 is your timezone.

Posted: Wed Jun 13, 2007 8:54 pm
by Flipper3
Isn't it also possible to do this?

Code: Select all

<?php
putenv('TZ=Europe/London');
?>

Like this is an example that you may try. ;)

Code: Select all

<?php
echo "Original Time: ". date("h:i:s")."\n";
putenv("TZ=US/Eastern");
echo "New Time: ". date("h:i:s")."\n";
?>

Posted: Thu Jun 14, 2007 4:27 am
by Lixas
i have wrote a funcion for myself to handle time difference on server and on my country

Code: Select all

$settings["server"]["time"] = "4"; //time differencie from your current time in hours

function showdate($val, $nosec = false, $line_count = "1", $conv=true)
    //show time int timestamp, bool hide_seconds, lines number
{
    global $settings;
    if($conv)
	{
		if ($val > "946677600")
    	{
    	    if ($line_count == "1")
    	    {
    	        if ($nosec == false)
    	            return strftime("%Y-%m-%d %H:%M:%S", $val + ($settings["server"]["time"] * 3600));
    	        elseif ($nosec == true)
					return strftime("%Y-%m-%d %H:%M", $val + ($settings["server"]["time"] * 3600));
			}
			else
        	{
            	if ($nosec == false)
            	    return strftime("%Y-%m-%d
%H:%M:%S", $val + ($settings["server"]["time"] * 3600));
	            elseif ($nosec == true)
    	            return strftime("%Y-%m-%d
%H:%M", $val + ($settings["server"]["time"] * 3600));
    	    }
    	}
    	else
        	return "<small>Wrong date code! " . ($val + ($settings["server"]["time"] * 3600)) . "</small>";
	}
	else
	{
		return strftime("%Y-%m-%d %H:%M", $val);
	}
    
}
#usage:
$time = showdate(time(), [hide_seconds: true or false], [line_count: 1 or 2], [fix time: true or false ]);

Posted: Thu Jun 14, 2007 7:17 am
by anish
Nice scripts. thanks man.

Posted: Wed Aug 01, 2007 6:50 am
by ami38701
and the best way is you make a function script for it,
which can call in timezon and call out the right time.. just like above.

Posted: Wed Sep 05, 2007 9:23 pm
by Gyanu
yes mine is 8