axelheyst Posted February 1, 2007 Share Posted February 1, 2007 I have the timezone offset I want (in this case it's "-5"). How can I change $dt=date('YmdHi'); so that it will reflect the offset. I tried manually breaking apart the hour component and simply adding or subtracting the value but then you have to deal with negative hours resulting i changing the day piece as well ... that seems like too much. Is there a way to exchange the integer offset ("-5") for the timezone formats that PHP accepts? Thanks all and have a good night! Link to comment https://forums.phpfreaks.com/topic/36593-solved-php-date-and-timezone-help/ Share on other sites More sharing options...
Jessica Posted February 1, 2007 Share Posted February 1, 2007 Use a timestamp, and subtract 5 hours, then convert to a string using date() Link to comment https://forums.phpfreaks.com/topic/36593-solved-php-date-and-timezone-help/#findComment-174290 Share on other sites More sharing options...
axelheyst Posted February 1, 2007 Author Share Posted February 1, 2007 Thank you. Using your suggestion I came up with this: $num=strlen($offset_tz); if($num==1){ $offset=$num; $sign='POS'; }elseif($num==2){ $first=substr($offset_tz, 0, 1); //does the string begin with a number? if(ctype_digit($first)){ $offset=substr($offset_tz, 0, 2); $sign='POS'; }else{ if($sign!='+'){ $sign='NEG'; $offset=substr($offset_tz, 1, 1); } } }elseif($num==3){ if(ctype_digit($first)){ $offset=substr($offset_tz, 0, 3); $sign='POS'; }else{ $offset=substr($offset_tz, 1, 2); $sign='NEG'; } } $unixtime=mktime(); if($sign=='NEG'){ $unixtime=$unixtime-(3600*$offset); }else{ $unixtime=$unixtime+(3600*$offset); } $dt=date('YmdHi',$unixtime); Link to comment https://forums.phpfreaks.com/topic/36593-solved-php-date-and-timezone-help/#findComment-174298 Share on other sites More sharing options...
Jessica Posted February 1, 2007 Share Posted February 1, 2007 Seems like overkill...Just add the offset... if you add a negative number, it subtracts. $time = time()+(3600*$offset); if offset is negative, it will subtract. I just tested it with this: <? $bar = '-5'; $foo = 100; print $foo+$bar; ?> And it printed 95. You're doing way too much work for simple math. Link to comment https://forums.phpfreaks.com/topic/36593-solved-php-date-and-timezone-help/#findComment-174304 Share on other sites More sharing options...
axelheyst Posted February 1, 2007 Author Share Posted February 1, 2007 Wow, much easier. Thank you. Link to comment https://forums.phpfreaks.com/topic/36593-solved-php-date-and-timezone-help/#findComment-174313 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.