On 05/04/2011 10:11, Simon J Welsh wrote:
> 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.
>
Hi,
Found the problem:
Unix timestamps are a moment in time and so timezones have no influences
on them. They count the number of seconds since 1970-01-01 00:00 *UTC*
See: http://en.wikipedia.org/wiki/Unix_time
So the timestamp will always be the same no matter which timezone.
I will use by workaround to get the expected timestamp for use in
comparisons.
Regards
Ian
--
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php