Re: [PHP] Date weirdness
On Thu, Mar 28, 2013 at 3:40 PM, Larry Martell wrote: > On Thu, Mar 28, 2013 at 2:44 PM, Steven Staples 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. I've upgraded to 5.3.3, got rid of the external Date extension and implement your solution. It's working perfectly. Thanks much! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date weirdness
On Thu, Mar 28, 2013 at 4:55 PM, Maciek Sokolewicz wrote: > On 28-3-2013 22:40, Larry Martell wrote: >> >> On Thu, Mar 28, 2013 at 2:44 PM, Steven Staples 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. >> > Well, if you're getting the Date class from a custom extension, then we > can't help you. Simply because we have no clue what the extension's code is. > > Also, please ask your host to upgrade, 5.1.6 is hopelessly outdated (and > unsupported!). The current version is 5.4 with 5.5 coming out very very > soon. > The extension is this: http://pear.php.net/package/Date/docs/latest/Date/Date.html - but that is also a very old version. I am working on getting the upgrade done. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date weirdness
On 28-3-2013 22:40, Larry Martell wrote: On Thu, Mar 28, 2013 at 2:44 PM, Steven Staples 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. Well, if you're getting the Date class from a custom extension, then we can't help you. Simply because we have no clue what the extension's code is. Also, please ask your host to upgrade, 5.1.6 is hopelessly outdated (and unsupported!). The current version is 5.4 with 5.5 coming out very very soon. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date weirdness
On Thu, Mar 28, 2013 at 2:44 PM, Steven Staples 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Date weirdness
> 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'); } Maybe this can help you? Steve -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] date weirdness
On Tue, 2007-09-18 at 15:32 -0500, Greg Donald wrote: > I have extracted a small portion of a calendar application I developed > recently to show some strange behavior with the date() function. When I > run this I get duplicate dates occasionally down the list. I'm seeing > 11/4 twice for example. Sometimes dates are missing from the list. The > results change from day to day. > > #!/usr/bin/env php > > error_reporting( E_ALL ); > > define( 'SECONDS_IN_A_DAY', 60 * 60 * 24 ); > define( 'LAST_SUNDAY', strtotime( 'last Sunday' ) ); > > echo 'SECONDS_IN_A_DAY = ' . SECONDS_IN_A_DAY . "\n"; > echo 'LAST_SUNDAY = ' . LAST_SUNDAY . "\n"; > > for( $x = 0; $x < 365; $x++ ) > { > $seconds = LAST_SUNDAY + ( SECONDS_IN_A_DAY * $x ); > $date = date( 'n/j', $seconds ); > echo "$seconds $date\n"; > } > ?> > > I get the same results with latest PHP4 and PHP5. Using seconds is a hack that doesn't account for DST. Use the following instead: Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] date weirdness
> I'm seeing > 11/4 twice for example. Sometimes dates are missing from the list. The > results change from day to day. > That would be DST. The number of seconds per day changes on Nov. 4, 2007 in the US local time zones. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php