coolbeansdude51 Posted March 8, 2009 Share Posted March 8, 2009 Alright. I have a time stamp that is UTC. Lets say its 13:54:40 UTC. The string is exactly "13:54:40" How can I convert that to lets say CST or in this case America\Chicago? I was thinking something like this: date_default_timezone_set('America\Chicago'); echo date('H:i:s', strtotime('13:54:40')); Unfortunately that just pumps out exactly the same time that I had before. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/ Share on other sites More sharing options...
corbin Posted March 8, 2009 Share Posted March 8, 2009 It should be America/Chicago. Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779339 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 <?php $time_adjust=time()+(0*?*0*0);//change '?' to the correct offset. $time=date(DATE_RFC822, $time_adjust); ?> Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779340 Share on other sites More sharing options...
coolbeansdude51 Posted March 8, 2009 Author Share Posted March 8, 2009 @corbin - I changed that. Still nothing. Anything else you can suggest? @WolfRage - could you explain that piece of code? I am guess that offset is either -6 or something like that ... correct? Thanks so much for your help so far! Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779346 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 Yes the current offset is -6. Basically time is a time stamp and the way to adjust it is by multiplying using this format (days * hours * mins *secs). Therefore you can adjust to meet any time by adjusting the hour offset, assuming that the server time is close to accurate. Date is how you make a date the first parameter is used to set it's format the second is your optional timestamp, because we are making an offset time, we want to pass our custom time stamp. To see date formats check here. http://us.php.net/manual/en/function.date.php Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779351 Share on other sites More sharing options...
coolbeansdude51 Posted March 8, 2009 Author Share Posted March 8, 2009 So where would the variable go with the time in it? Would it be like this? <?php $time_adjust=$time1+(0*?*0*0);//change '?' to the correct offset. $time=date(DATE_RFC822, $time_adjust); ?> Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779358 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 I thought you wanted to just generate proper timestamps, according to your time. But adjusting an existing date is a little more complex. You will need to use the DateTime Class, and then use the add function. <?php $date = new DateTime(//your pre-existing timestamp goes here.); $chicago_time=date_add($date, new DateInterval("T-6H")); ?> http://us2.php.net/manual/en/datetime.add.php Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779375 Share on other sites More sharing options...
coolbeansdude51 Posted March 8, 2009 Author Share Posted March 8, 2009 For some reason I can't get the DateTime class to work ... am I including it correctly? time.class.php <?PHP class DateTime { /* Constants */ const string DateTime::ATOM = Y-m-d\TH:i:sP ; const string DateTime::COOKIE = l, d-M-y H:i:s T ; const string DateTime::ISO8601 = Y-m-d\TH:i:sO ; const string DateTime::RFC822 = D, d M y H:i:s O ; const string DateTime::RFC850 = l, d-M-y H:i:s T ; const string DateTime::RFC1036 = D, d M y H:i:s O ; const string DateTime::RFC1123 = D, d M Y H:i:s O ; const string DateTime::RFC2822 = D, d M Y H:i:s O ; const string DateTime::RFC3339 = Y-m-d\TH:i:sP ; const string DateTime::RSS = D, d M Y H:i:s O ; const string DateTime::W3C = Y-m-d\TH:i:sP ; /* Methods */ public DateTime add ( string $interval ) __construct ([ string $time= "now" [, DateTimeZone $timezone= NULL ]] ) public static DateTime createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] ) public DateInterval diff ( DateTime $datetime [, bool $absolute ] ) public string format ( string $format ) public static array getLastErrors ( void ) public int getOffset ( void ) public int getTimestamp ( void ) public DateTimeZone getTimezone ( void ) public DateTime modify ( string $modify ) public static DateTime __set_state ( array $array ) public DateTime setDate ( int $year , int $month , int $day ) public DateTime setISODate ( int $year , int $week [, int $day ] ) public DateTime setTime ( int $hour , int $minute [, int $second ] ) public DateTime setTimestamp ( int $unixtimestamp ) public DateTime setTimezone ( DateTimeZone $timezone ) public DateTime sub ( DateInterval $interval ) public DateTime __wakeup ( void ) } ?> Then I just do a require('time.class.php') on it. Is that correct? Thanks for your help so far! Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779395 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 Try changing it to this. <?php $date = new DateTime(//your pre-existing timestamp goes here.); $chicago_time=date_add($this->date, new DateInterval("T-6H")); ?> Can you post your code to call the class and to convert the time. Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779408 Share on other sites More sharing options...
trq Posted March 8, 2009 Share Posted March 8, 2009 Wherever you got that DateTime class from, its not php. Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779410 Share on other sites More sharing options...
coolbeansdude51 Posted March 8, 2009 Author Share Posted March 8, 2009 Wherever you got that DateTime class from, its not php. @thorpe -- Really? I pulled it from here. http://www.php.net/manual/en/class.datetime.php @WolfRage -- to include the class I do this: require 'class.DateTime.php'; to convert the time I do this: $date = new DateTime('10:15:54'); $chicago_time=date_add($this->date, new DateInterval("T-6H")); Is that incorrect? I am new with this whole class thing. Thanks for the help and the patience! Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779414 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 What version of php are you running? Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779416 Share on other sites More sharing options...
coolbeansdude51 Posted March 8, 2009 Author Share Posted March 8, 2009 5.2.8 Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779418 Share on other sites More sharing options...
trq Posted March 8, 2009 Share Posted March 8, 2009 @thorpe -- Really? I pulled it from here. http://www.php.net/manual/en/class.datetime.php Yeah, that is documention. Its describing the actual DateTime object built into php. You don't need to include it into anything. Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779419 Share on other sites More sharing options...
coolbeansdude51 Posted March 8, 2009 Author Share Posted March 8, 2009 @thorpe -- Really? I pulled it from here. http://www.php.net/manual/en/class.datetime.php Yeah, that is documention. Its describing the actual DateTime object built into php. You don't need to include it into anything. Doah! Ok. I feel stupid now. Ok. I deleted the include for that. Now it error's out with this Fatal error: Call to undefined function date_add() in index.php on line 54 Any ideas? Thanks Thrope for your help and clarity! Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779422 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 I have an eaiser way around all of this give me a second. Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779427 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 Ok I know this is a round about way of doing it, but it works. <?php $broken_time=explode(':','10:56:45'); $time=date('H:i:s',mktime(-6+$broken_time[0],0+$broken_time[1],0+$broken_time[2],0,0,0)); echo $time; ?> Also you can modify this to meet your needs. '-6' could be made into a variable to represent the offset time. Oh an make sure you modify the '10:56:45' to an actual variable which will represent your time stamps. Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779428 Share on other sites More sharing options...
coolbeansdude51 Posted March 8, 2009 Author Share Posted March 8, 2009 Ok I know this is a round about way of doing it, but it works. <?php $broken_time=explode(':','10:56:45'); $time=date('H:i:s',mktime(-6+$broken_time[0],0+$broken_time[1],0+$broken_time[2],0,0,0)); echo $time; ?> Also you can modify this to meet your needs. '-6' could be made into a variable to represent the offset time. Oh an make sure you modify the '10:56:45' to an actual variable which will represent your time stamps. That does worked! Thanks so much for your help! Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-779433 Share on other sites More sharing options...
coolbeansdude51 Posted March 9, 2009 Author Share Posted March 9, 2009 CRAP!! Ok. Something which I didn't take into account was the timezones which are 6.5 or something like that. Does this function take that into account? It doesn't look like it does. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-780110 Share on other sites More sharing options...
WolfRage Posted March 9, 2009 Share Posted March 9, 2009 OK we can adjust it to meet that need. Just subtract another 30 minutes like so. <?php $broken_time=explode(':','10:56:45'); $time=date('H:i:s',mktime(-6+$broken_time[0],-30+$broken_time[1],0+$broken_time[2],0,0,0)); echo $time; ?> Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-780186 Share on other sites More sharing options...
coolbeansdude51 Posted March 9, 2009 Author Share Posted March 9, 2009 Right ... but its a variable ... Its a post variable technically. $tz = -6 Thanks so much for the help so far! Just his one last thing has been bugging me! Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-780276 Share on other sites More sharing options...
WolfRage Posted March 9, 2009 Share Posted March 9, 2009 OK, Lets do this then and make it fully customizable, and understandable. <?php $broken_time=explode(':','10:56:45'); $tz=-6; if(is_int($tz)) { $time=date('H:i:s',mktime($tz+$broken_time[0],0+$broken_time[1],0+$broken_time[2],0,0,0)); } else { //it must be a double aka float $broken_zone=explode('.',$tz); $broken_zone[0]=($broken_zone[0]++)-1; //I know that there must be a better way to do this but hell it works. $broken_zone[1]=((($broken_zone[1]++)-1)*0.1)*60; $time=date('H:i:s',mktime($broken_zone[0]+$broken_time[0],$broken_zone[1]+$broken_time[1],0+$broken_time[2],0,0,0)); } echo $time; ?> I amuse myself, with some of the things I come up with. I hope that this helps you out. Let me know; and let me know if you have any more customization that you need. This solution will work as long as you do not have more than two decimal points in $tz . Link to comment https://forums.phpfreaks.com/topic/148444-timezone-conversion/#findComment-780305 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.