Jump to content

[SOLVED] PHP Date and Timezone Help


axelheyst

Recommended Posts

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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.