Re: DateTime using DateTimeZone Timestamp problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 2011-04-04 at 16:35 +0100, Ian wrote:

> Hi,
> 
> I have a problem using the php built in classes DateTime and DateTimeZone.
> 
> The idea behind the following code is to return the timestamp for the
> current time in Singapore (or other places).  What it actually returns
> is the timestamp for the local system. Other formatted dates appear to
> return correctly, which is why I am puzzled.
> 
> I am using the latest php 5.3.6 compiled from source on a OpenVZ CentOS
> container. All packages are up to date.
> 
> Am I doing something wrong or is this a bug?
> 
> I can workaround this problem my parsing the correctly formatted date
> using strtotime() but I would like to know what's going on.
> 
> 
> 
> This is the output of the script:
> 
> 	Current time in Asia/Singapore is 2011-04-04 23:32:36
> 	Timestamp for Asia/Singapore is 1301931156
> 	Date created from previous timestamp is 2011-04-04 16:32:36
> 
> The code is :
> 
> <?php
> 
> $timezone="Asia/Singapore";
> 
> # Create Timezone object
> $remote_timezone	= new DateTimeZone($timezone);
> 
> # Create datetime object
> $remote_time 		= new DateTime("now" , $remote_timezone);
> 
> # Print the date
> print "Current time in {$timezone} ";
> print "is {$remote_time->format("Y-m-d H:i:s")}<br/>";
> 
> # Print the timestamp
> print "Timestamp for {$timezone} ";
> print "is {$remote_time->format("U")}<br />";
> 
> # Get the timestamp and create a date from it
> $timestamp = (int)$remote_time->format("U");
> 
> # Show the formatted date created from timestamp
> print "Date created from previous timestamp is ";
> print date("Y-m-d H:i:s",$timestamp)."<br/>";
> 
> ?>
> 
> 
> 


What do you mean it only returns the timestamp for the local system? Did
you want PHP to know what time the visitors are on? PHP won't know about
that, all you can do is set the timezone for the script based on some
information you're receiving from a clients machine, otherwise PHP won't
know, because it's only run on the server and doesn't know about the
client machines? Is this what you're trying to do, or did I
misunderstand?

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux