Jump to content

Simple strtotime() converstion with offset


Shadowing

Recommended Posts

Trying to convert my UNIX time into a users offset time. Gathering the users offset in this format -1200

 

so I was reading about the date function in the PHP manual. but all the examples are using location words as offsets

 

string date ( string $format [, int $timestamp = time() ] )

 

cant I just do $time = strtotime('-1200')

 

I know i saw the conversion for this somewhere in this forum few days back but cant find it now.

Im only wanting to convert the time when displaying it to the user only.

Link to comment
Share on other sites

ok want to make sure I understand this right

since my server is -6 i cant use my user inputed offset cause it would be going against my -6

so i have to set it to UTC which is 0. so when someone has -12 it will be really -12 when comparing it to time().

 

when I do date_default_timezone_set('UTC');

does that change the server time to UTC always? or is that only for that script that is currently running

 

I read someone in the forum where someone couldnt use the date_default_timezone_set('UTC'); cause it was dissallowed on the server. right now im using local host but will upload to a live host one day and id hate to be using something that wont work on a live server

Link to comment
Share on other sites

date_default_timezone_set affects only that script.  If you wanted to affect every php script you would have to set the timezone in the php.ini file.  The server's configured timezone is only used as a last resort.  The latest PHP versions will complain with a warning if you don't set the timezone explicitly either via the php.ini setting or the date_default_timezone_set function.

 

Link to comment
Share on other sites

thanks kicken big help

is this how I do hour minute and second too?

 

date('m/d/Y/H:i:s', time()-1200);

 

also can I turn this as a variable like this

 

$timezone = date('m/d/Y/H:i:s', time()-1200);

 

and then echo it

 

echo "This is your time $timezone";

 

am I doing this right?

Link to comment
Share on other sites

This isnt working for me for some reason

its almost 7 hours off

and the minutes are like 5 minutes off

 

 

date_default_timezone_set('UTC');

 

$new = date('m/d/Y/H:i:s', time()-0600);

 

echo $new;

 

 

 

and if I only do this "with out the UTC set" its not even giving me my own time. 10 hours off

 

$new = date('m/d/Y/H:i:s', time());

 

echo $new;

Link to comment
Share on other sites

Alright I found out whats happending. i was wrong. This works fine its showing GMT time

 

date_default_timezone_set('UTC');

$new = date('m/d/Y/H:i:s', time());

 

 

but when I add this it doesnt work. it subtracts 6 minutes and not 6 hours

 

$new = date('m/d/Y/H:i:s', time()-0600);

 

 

anyone know what im doing wrong here?

Link to comment
Share on other sites

Alright I found something that works

apparently using time doesnt work had to replace it with strotime.

will daylight saving time get adjusted according for this?

 

date_default_timezone_set('UTC');

 

$new = date('m/d/Y/H:i:s', strtotime("-0600"));

 

echo $new;

Link to comment
Share on other sites

but when I add this it doesnt work. it subtracts 6 minutes and not 6 hours

 

The return value of time() is in seconds.  Thus, your offset needs to be in number of seconds as well.  Just use a little math to turn your 6 hours into seconds.

 

Also, prefixing a number with  a 0 causes it to be interpreted as an octal value which is not what you want.  Remove the leading zero prefix.

Link to comment
Share on other sites

I came up with a solution maybe

since its not adding in  the offset. i could apply the offset first and then format it

but my $time variable isnt doing the offset

 

 <?php                                                 $recieved = $row['time'];
		                       $time = ($recieved + $send1['time_offset']);

date_default_timezone_set('UTC');

$offset = date('m/d/Y/H:i:s', $time);  ?> 

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.