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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.