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! Quote Link to comment 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() Quote Link to comment 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); Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
axelheyst Posted February 1, 2007 Author Share Posted February 1, 2007 Wow, much easier. Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.