Jump to content

Timezone conversion


coolbeansdude51

Recommended Posts

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

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

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

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

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

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

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

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

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