Re: [PHP] DateTime using DateTimeZone Timestamp problem

2011-04-05 Thread Simon J Welsh
On 5/04/2011, at 3:35 AM, 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/;
 
 ?

May I suggest including the timezone in your date format (O or e)? It may show 
the two date strings to be equivalent.

---
Simon Welsh
Admin of http://simon.geek.nz/

Who said Microsoft never created a bug-free program? The blue screen never, 
ever crashes!

http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] DateTime using DateTimeZone Timestamp problem

2011-04-05 Thread Richard Quadling
On 4 April 2011 16:35, Ian php_l...@fishnet.co.uk 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/;

 ?

Timestamps (the integer value) do not hold the timezone data.
Internally, the value represents a number of milliseconds from a point
in time.

So saying timestamp for Asia/Singapore isn't right. It is just Timestamp.

The following script (http://pastebin.com/0MQAaYUq) may show you in a
more concrete way ...

?php
$a_Times = array(
'now',
'2011-03-27 00:59:59',
'2011-03-27 02:00:00',
);

// Create Timezone objects
$a_Timezones = array(
'Singapore' = new DateTimeZone('Asia/Singapore'),
'NewYork  ' = new DateTimeZone('America/New_York'),
'London   ' = new DateTimeZone('Europe/London'),
'UTC  ' = new DateTimeZone('UTC'),
);

foreach($a_Times as $s_Time) {
echo 'Time : ', $s_Time, PHP_EOL;

// Create datetime objects
$a_DateTimes = array();
foreach($a_Timezones as $s_Timezone = $tz_Timezone) {
$a_DateTimes[$s_Timezone] = new DateTime($s_Time , 
$tz_Timezone);
}

// Print the date
foreach($a_DateTimes as $s_Timezone = $dt_DateTime) {
echo
'Current time in ', $s_Timezone, ' : ', 
$dt_DateTime-format(DateTime::RSS),
'   Offset : ', str_pad($dt_DateTime-getOffset(), 6, ' 
', STR_PAD_LEFT),
'   Timestamp : ', ($i_Timestamp = 
$dt_DateTime-getTimestamp()),
'   Local : ', date(DateTime::RSS, $i_Timestamp), 
PHP_EOL;
}
echo PHP_EOL;
}
?

outputs (http://pastebin.com/mETSbR7h) ...

Time : now
Current time in Singapore : Tue, 05 Apr 2011 17:56:32 +0800   Offset :
 28800   Timestamp : 1301997392   Local : Tue, 05 Apr 2011 10:56:32
+0100
Current time in NewYork   : Tue, 05 Apr 2011 05:56:32 -0400   Offset :
-14400   Timestamp : 1301997392   Local : Tue, 05 Apr 2011 10:56:32
+0100
Current time in London: Tue, 05 Apr 2011 10:56:32 +0100   Offset :
  3600   Timestamp : 1301997392   Local : Tue, 05 Apr 2011 10:56:32
+0100
Current time in UTC   : Tue, 05 Apr 2011 09:56:32 +   Offset :
 0   Timestamp : 1301997392   Local : Tue, 05 Apr 2011 10:56:32
+0100

Time : 2011-03-27 00:59:59
Current time in Singapore : Sun, 27 Mar 2011 00:59:59 +0800   Offset :
 28800   Timestamp : 1301158799   Local : Sat, 26 Mar 2011 16:59:59
+
Current time in NewYork   : Sun, 27 Mar 2011 00:59:59 -0400   Offset :
-14400   Timestamp : 1301201999   Local : Sun, 27 Mar 2011 05:59:59
+0100
Current time in London: Sun, 27 Mar 2011 00:59:59 +   Offset :
 0   Timestamp : 1301187599   Local : Sun, 27 Mar 2011 00:59:59
+
Current time in UTC   : Sun, 27 Mar 2011 00:59:59 +   Offset :
 0   Timestamp : 1301187599   Local : Sun, 27 Mar 2011 00:59:59
+

Time : 2011-03-27 02:00:00
Current time in Singapore : Sun, 27 Mar 2011 02:00:00 +0800   Offset :
 28800   Timestamp : 1301162400   Local : Sat, 26 Mar 2011 18:00:00
+
Current time in NewYork   : Sun, 27 Mar 2011 02:00:00 -0400   Offset :
-14400   Timestamp : 1301205600   Local : Sun, 27 Mar 2011 07:00:00
+0100
Current time in London: Sun, 27 Mar 2011 02:00:00 +0100   Offset :
  3600   Timestamp : 1301187600   Local : Sun, 27 Mar 2011 02:00:00
+0100
Current time in UTC   : Sun, 27 Mar 2011 02:00:00 +   Offset :
 0   Timestamp : 1301191200   Local : Sun, 27 Mar 2011 03:00:00
+0100


Getting the the timestamp for a DateTime object 

Re: [PHP] DateTime using DateTimeZone Timestamp problem

2011-04-04 Thread Ashley Sheridan
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