PDA

View Full Version : Get UTC Time



Troy297
08-25-2007, 10:24 PM
Hey All,

I'm just wondering how I might go about getting the UTC time with PHP. I currently have a Javascript that returns the UTC time which I use in a clock format with a user-defined offset so now I need to do the same in php and then I'll just use the same offset.

Any help is great! Thanks!

Note: I need a code snippet that doesn't use the following:
date_default_timezone_set()
putenv()

kiwibrit
08-26-2007, 12:27 AM
gmdate() (http://uk.php.net/gmdate)

Troy297
08-27-2007, 02:33 AM
Thanks :) Thats doing the trick for me and its working fine....

Now another thing semi-related... I am trying to use a user-defined offset to add or subtract from the output gmdate("H") but the problem is now that the adding and subtracting doesn't know its dealing with time and therefore I get these funny integers for the hours instead of proper times... any help?

My Syntax:

$date = gmdate("M j, Y @ g:i a");
$rdate = gmdate("Y-m-d H:i:s");
$ipaddr = $_SERVER['REMOTE_ADDR'];
$to = mysql_query("SELECT timeoff FROM rp4_settings");
$row = mysql_fetch_array($to);
$to2 = $row['timeoff'];
$th = gmdate("H");
$t2 = gmdate("i");
$t3 = gmdate("s");
if($to2 == "-1"|$to2 == "-2"|$to2 == "-3"|$to2 == "-4"|$to2 == "-5"|$to2 == "-6"|$to2 == "-7"|$to2 == "-8"|$to2 == "-9"|$to2 == "-10"|$to2 == "-11"|$to2 == "-12"){
$tn = str_replace("-", "", $to2);
$t1 = $th-$tn;
}
if($to2 == "+1"|$to2 == "+2"|$to2 == "+3"|$to2 == "+4"|$to2 == "+5"|$to2 == "+6"|$to2 == "+7"|$to2 == "+8"|$to2 == "+9"|$to2 == "+10"|$to2 == "+11"|$to2 == "+12"){
$tn = str_replace("+", "", $to2);
$t1 = $th+$tn;
}elseif($to2 == "0"|$to2 == ""){
$t1 = gmdate("H:i:s");
}
$dy = gmdate("Y-m-d");
$t4 = $t1.":".$t2.":".$t3;

Note: The "timeoff" field is in the following format; -4 (either a +/- then either one or two numbers)

Thanks and rep will be given!

Inigoesdr
08-27-2007, 02:53 AM
$tz = -10;
list($month, $day, $year, $hour, $minute, $second) = explode(' ', gmdate('n j Y G i s'));
$old = gmdate('r', gmmktime($hour, $minute, $second, $month, $day, $year));
$newdate = gmdate('r', gmmktime($hour + $tz, $minute, $second, $month, $day, $year));
echo $old . "<br />" . $newdate;
You don't need $old, that was just to see the difference.

Troy297
08-27-2007, 04:22 AM
Thanks! That code snippet works perfectly for what I need... now one more thing...

Although this kinda isn't PHP it is still part of this whole issue that this thread deals with so here goes....

Why do I need an offset of -8 for this to display the proper time when I'm really only -4 GMT?


function jsClockTimeZone(){
// Copyright 1999 - 2001 by Ray Stott
// Script available at http://www.crays.com/jsc
var TimezoneOffset = <?php echo $timeoff;?>
var localTime = new Date()
var ms = localTime.getTime()
+ (localTime.getTimezoneOffset() * 60000)
+ TimezoneOffset * 3600000
var time = new Date(ms)
var hour = time.getUTCHours()
var minute = time.getUTCMinutes()
var second = time.getUTCSeconds()
var temp = "" + ((hour > 12) ? hour - 12 : hour)
if(hour==0) temp = "12"
temp += ((minute < 10) ? ":0" : ":") + minute
temp += ((second < 10) ? ":0" : ":") + second
temp += (hour >= 12) ? " PM" : " AM"
document.clockFormTimeZone.digits.value = temp
setTimeout("jsClockTimeZone()",1000)
}

Thanks again!

EDIT: One other very weird thing... using the script provided by Inigoesdr the time (minutes) seemed to be about 20 minutes ahead of the actual time....?