Shadowing Posted December 21, 2011 Share Posted December 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/ Share on other sites More sharing options...
kicken Posted December 21, 2011 Share Posted December 21, 2011 Just add/subtract the users offset from your timestamp. You'd probably want to set your script to use the UTC timezone by default. date_default_timezone_set('UTC'); date('m/d/Y', time()-1200); Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300011 Share on other sites More sharing options...
Shadowing Posted December 21, 2011 Author Share Posted December 21, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300017 Share on other sites More sharing options...
kicken Posted December 21, 2011 Share Posted December 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300019 Share on other sites More sharing options...
Shadowing Posted December 21, 2011 Author Share Posted December 21, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300020 Share on other sites More sharing options...
shankarganesh Posted December 21, 2011 Share Posted December 21, 2011 Hi, Use this to reduce the hours and minutes, $unix_time = strtotime('January 1 2010 '.$time); // create a unix timestamp echo date( "H:i", strtotime('-30 minutes', $unix_time) ); Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300032 Share on other sites More sharing options...
Shadowing Posted December 21, 2011 Author Share Posted December 21, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300195 Share on other sites More sharing options...
Shadowing Posted December 21, 2011 Author Share Posted December 21, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300235 Share on other sites More sharing options...
Shadowing Posted December 21, 2011 Author Share Posted December 21, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300242 Share on other sites More sharing options...
kicken Posted December 21, 2011 Share Posted December 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300243 Share on other sites More sharing options...
Shadowing Posted December 22, 2011 Author Share Posted December 22, 2011 thanks alot Kicken i got it working but now I have a snytax issue when inserting it the seconds into this $new = date('m/d/Y/H:i:s', time()$send1['time_offset']); how did you solve that? Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300345 Share on other sites More sharing options...
Shadowing Posted December 22, 2011 Author Share Posted December 22, 2011 ahh I figured it out i had to add a + sign $new = date('m/d/Y/H:i:s', time()+$send1['time_offset']); Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300362 Share on other sites More sharing options...
Shadowing Posted December 23, 2011 Author Share Posted December 23, 2011 Hey Kicken how do you add a variable in to offset the variable $recieved $recieved is a stored Unix time in mysql that i want to add the offset to <?php $offset = date('m/d/Y/H:i:s', time()+$send1['time_offset']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300918 Share on other sites More sharing options...
kicken Posted December 23, 2011 Share Posted December 23, 2011 Just replace time() with the variable. $offset = date('m/d/Y/H:i:s', $received+$send1['time_offset']); Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300938 Share on other sites More sharing options...
Shadowing Posted December 23, 2011 Author Share Posted December 23, 2011 thanks for the responce yah i tried that gives me the wrong time. its 6 hours off which is exactly waht the offset is hmm so its not addig the time offset in. so maybe the + is wrong Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300948 Share on other sites More sharing options...
Shadowing Posted December 23, 2011 Author Share Posted December 23, 2011 is there a way to just make time function into my variable? so when I use time it equals the variable tried alot of combinations with changing the + with stuff and using () Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300963 Share on other sites More sharing options...
Shadowing Posted December 23, 2011 Author Share Posted December 23, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1300970 Share on other sites More sharing options...
Shadowing Posted December 24, 2011 Author Share Posted December 24, 2011 I got the code to work in my last reply what was wrong was $send1 was suppose to be $take1 lol. but this code out puts 12/31/1969/18:00:00 <?php $offset = date('m/d/Y/H:i:s', $received+$take1['time_offset']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/253593-simple-strtotime-converstion-with-offset/#findComment-1301078 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.