Re: Can i use DateTime Object with an timestamp as referenceinstead of an formated string.

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

 



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();
			}
		}
	}
}

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


[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