Re: Date weirdness

From: Date: Thu, 28 Mar 2013 21:40:41 +0000
Subject: Re: Date weirdness
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-320715@lists.php.net to get a copy of this message
On Thu, Mar 28, 2013 at 2:44 PM, Steven Staples <sstaples@mnsi.net> wrote: >> I think I am losing my mind. I have some time zone converting code, and I >> just don't understand what I am seeing. Also my system seems to return the >> wrong time after I do some date operations on unrelated objects. >> >> This is from a machine that is in eastern time. I want to convert to, for >> example central time: >> >> $ndate = new Date(date("Y-m-d H:i:s")); >> echo $ndate->format("%Y-%m-%d %H:%M:%S"); >> 2013-03-28 15:35:07 <- this is the correct time >> >> $ndate->setTZbyID("EDT"); >> echo $ndate->format("%Y-%m-%d %H:%M:%S"); >> 2013-03-28 15:35:07 <- still correct >> >> $ndate->convertTZbyID("US/Central"); >> echo $ndate->format("%Y-%m-%d %H:%M:%S"); >> 2013-03-28 10:35:07 <- this is wrong it should be 14:35:07 >> >> $xdate = new Date(date("Y-m-d H:i:s")); >> echo $xdate->format("%Y-%m-%d %H:%M:%S"); >> 2013-03-28 19:35:07 <- HUH? This is wrong - should be 15:35:07 >> >> What in the world is going on here? >> > > I found this function a while back when I was converting UTC to EST... simple task I know, but > still... > > ( I am sorry to whomever wrote this, I didn't keep the source where I found it ) > > function convert_time_zone($date_time, $from_tz = 'UTC', $to_tz = > 'America/Toronto') > { > $time_object = new DateTime($date_time, new DateTimeZone($from_tz)); > $time_object->setTimezone(new DateTimeZone($to_tz)); > return $time_object->format('Y-m-d H:i:s'); > } I don't seem to have the DateTime object. We are running 5.1.6 and that was added in 5.2.0. We are getting the Date module from an external extension. I'll have to see about upgrading.

« previous php.general (#320715) next »