Adjust server time in PHP

Any problem with PHP can be disscused here
Post Reply
blue-sky
Posts: 33
Joined: Tue Jun 12, 2007 3:41 pm

Adjust server time in PHP

Post 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.


Flipper3
Posts: 353
Joined: Tue Feb 28, 2006 12:34 am

Post 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";
?>
Lixas
Posts: 750
Joined: Wed Feb 16, 2005 4:21 pm

Post 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 ]);
Image
anish
Posts: 353
Joined: Fri Apr 27, 2007 12:34 pm
Contact:

Post by anish »

Nice scripts. thanks man.
ami38701
Posts: 62
Joined: Wed Aug 01, 2007 6:42 am

Post 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.
Gyanu
Posts: 338
Joined: Mon Jul 30, 2007 2:03 pm
Contact:

Post by Gyanu »

yes mine is 8
Image
Post Reply