Can i use DateTime Object with an timestamp as reference insteadof an formated string.

This is a discussion on Can i use DateTime Object with an timestamp as reference insteadof an formated string. within the PHP General forums, part of the PHP Programming Forums category; Hello there, I Use timestamps for logging etc.. But i need this timestamp to let is show the right time ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-07-2007
Mathijs
 
Posts: n/a
Default Can i use DateTime Object with an timestamp as reference insteadof an formated string.

Hello there,

I Use timestamps for logging etc..
But i need this timestamp to let is show the right time in different
timezones.

Now i wanted to use DateTime object, but it seems it needs a string as
reference, so i can't use a timestamp to re-generate that time.

How can i fix this?

Thx in advance.
Reply With Quote
  #2 (permalink)  
Old 03-07-2007
Németh Zoltán
 
Posts: n/a
Default Re: [PHP] Can i use DateTime Object with an timestamp as referenceinstead of an formated string.

2007. 03. 7, szerda keltezéssel 12.58-kor Mathijs ezt Ã*rta:
> Hello there,
>
> I Use timestamps for logging etc..
> But i need this timestamp to let is show the right time in different
> timezones.
>
> Now i wanted to use DateTime object, but it seems it needs a string as
> reference, so i can't use a timestamp to re-generate that time.
>
> How can i fix this?


I think
http://hu2.php.net/manual/hu/function.strftime.php
should be okay

greets
Zoltán Németh

>
> Thx in advance.
>

Reply With Quote
  #3 (permalink)  
Old 03-07-2007
Mathijs
 
Posts: n/a
Default Re: [PHP] Can i use DateTime Object with an timestamp as referenceinsteadof an formated string.

Németh Zoltán wrote:
> 2007. 03. 7, szerda keltezéssel 12.58-kor Mathijs ezt Ã*rta:
>> Hello there,
>>
>> I Use timestamps for logging etc..
>> But i need this timestamp to let is show the right time in different
>> timezones.
>>
>> Now i wanted to use DateTime object, but it seems it needs a string as
>> reference, so i can't use a timestamp to re-generate that time.
>>
>> How can i fix this?

>
> I think
> http://hu2.php.net/manual/hu/function.strftime.php
> should be okay
>
> greets
> Zoltán Németh
>
>> Thx in advance.
>>


I hoped to avoid this.
I Now i need an extra function call to get the right time.
Hoped there was a way to give just the timestamp directly.
Reply With Quote
  #4 (permalink)  
Old 03-07-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Can i use DateTime Object with an timestamp as referenceinstead of an formated string.

Mathijs wrote:
> Hello there,
>
> I Use timestamps for logging etc..


so you do something like this no?:

$stamp = mktime();

so if you want to use DateTime why not do this?:

$stamp = new DateTime;

alternatively if you are stuck with the original integer timestamp,
then you can get round the issue like so:


$stamp = mktime();
$date = getdate($stamp);

$obj = new DateTime;
$obj->setDate($date['year'], $date['mon'], $date['mday']);
$obj->setTime($date['hours'], $date['minutes'], $date['seconds']);
$obj->setTimezone('er?');

or something like that.

seems rather stupid that DateTime doesn't accept a timestamp, but there is
probably a good reason ... as Richard Lynch pointed out date/time stuff seems
easy but is actually very very hard (and something computers will probably
never get 'right')

anyway, everything you need is right here if care to read it:

http://php.net/manual/en/ref.datetime.php

> But i need this timestamp to let is show the right time in different
> timezones.
>
> Now i wanted to use DateTime object, but it seems it needs a string as
> reference, so i can't use a timestamp to re-generate that time.
>
> How can i fix this?
>
> Thx in advance.
>

Reply With Quote
  #5 (permalink)  
Old 03-07-2007
Mathijs
 
Posts: n/a
Default Re: [PHP] Can i use DateTime Object with an timestamp as referenceinsteadof an formated string.

Jochem Maas wrote:
> Mathijs wrote:
>> Hello there,
>>
>> I Use timestamps for logging etc..

>
> so you do something like this no?:
>
> $stamp = mktime();
>
> so if you want to use DateTime why not do this?:
>
> $stamp = new DateTime;
>
> alternatively if you are stuck with the original integer timestamp,
> then you can get round the issue like so:
>
>
> $stamp = mktime();
> $date = getdate($stamp);
>
> $obj = new DateTime;
> $obj->setDate($date['year'], $date['mon'], $date['mday']);
> $obj->setTime($date['hours'], $date['minutes'], $date['seconds']);
> $obj->setTimezone('er?');
>
> or something like that.
>
> seems rather stupid that DateTime doesn't accept a timestamp, but there is
> probably a good reason ... as Richard Lynch pointed out date/time stuff seems
> easy but is actually very very hard (and something computers will probably
> never get 'right')
>
> anyway, everything you need is right here if care to read it:
>
> http://php.net/manual/en/ref.datetime.php
>
>> But i need this timestamp to let is show the right time in different
>> timezones.
>>
>> Now i wanted to use DateTime object, but it seems it needs a string as
>> reference, so i can't use a timestamp to re-generate that time.
>>
>> How can i fix this?
>>
>> Thx in advance.
>>


Thx for that idea.
I Now created an class/object which extends from DateTime.
Hope this can help other ppl with the same prob as well.

Thx.
Mathijs.


class DateTimeHandler extends DateTime {
public final function __construct($time=null, DateTimeZone $object=null) {
if (is_numeric($time)) {
parent::__construct();
$datetime = getdate($time);
parent::setDate($datetime['year'], $datetime['mon'], $datetime['mday']);
parent::setTime($datetime['hours'], $datetime['minutes'],
$datetime['seconds']);
parent::setTimezone($object);
return $this;
} else {
if ($object) {
return parent::__construct($time, $object);
} else {
return parent::__construct();
}
}
}
}
Reply With Quote
  #6 (permalink)  
Old 03-13-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] Can i use DateTime Object with an timestamp asreferenceinstead of an formated string.

Personally, I'd log it in GMT or UTC or whatever it is, and then only
change time-zones on display.

Logging various different time-zones within the actualy data is just
going to be confusing, probably...

Depends on what you are doing, though, I guess...

YMMV

On Wed, March 7, 2007 7:12 am, Mathijs wrote:
> Németh Zoltán wrote:
>> 2007. 03. 7, szerda keltezéssel 12.58-kor Mathijs ezt Ã*rta:
>>> Hello there,
>>>
>>> I Use timestamps for logging etc..
>>> But i need this timestamp to let is show the right time in
>>> different
>>> timezones.
>>>
>>> Now i wanted to use DateTime object, but it seems it needs a string
>>> as
>>> reference, so i can't use a timestamp to re-generate that time.
>>>
>>> How can i fix this?

>>
>> I think
>> http://hu2.php.net/manual/hu/function.strftime.php
>> should be okay
>>
>> greets
>> Zoltán Németh
>>
>>> Thx in advance.
>>>

>
> I hoped to avoid this.
> I Now i need an extra function call to get the right time.
> Hoped there was a way to give just the timestamp directly.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
Reply With Quote
  #7 (permalink)  
Old 03-14-2007
Mathijs
 
Posts: n/a
Default Re: [PHP] Can i use DateTime Object with an timestamp as referenceinsteadof an formated string.

Richard Lynch wrote:
> Personally, I'd log it in GMT or UTC or whatever it is, and then only
> change time-zones on display.
>
> Logging various different time-zones within the actualy data is just
> going to be confusing, probably...
>
> Depends on what you are doing, though, I guess...
>
> YMMV
>
> On Wed, March 7, 2007 7:12 am, Mathijs wrote:
>> Németh Zoltán wrote:
>>> 2007. 03. 7, szerda keltezéssel 12.58-kor Mathijs ezt Ã*rta:
>>>> Hello there,
>>>>
>>>> I Use timestamps for logging etc..
>>>> But i need this timestamp to let is show the right time in
>>>> different
>>>> timezones.
>>>>
>>>> Now i wanted to use DateTime object, but it seems it needs a string
>>>> as
>>>> reference, so i can't use a timestamp to re-generate that time.
>>>>
>>>> How can i fix this?
>>> I think
>>> http://hu2.php.net/manual/hu/function.strftime.php
>>> should be okay
>>>
>>> greets
>>> Zoltán Németh
>>>
>>>> Thx in advance.
>>>>

>> I hoped to avoid this.
>> I Now i need an extra function call to get the right time.
>> Hoped there was a way to give just the timestamp directly.
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>

>
>


I use this for displaying and saving the right timestamp when a user
enters a date/time.
I Save it all as our global timestamp, so that shouldn't be a problem.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:21 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0