Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Get UTC Time

  1. #1
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Question Get UTC Time

    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()
    Last edited by Troy297; 08-25-2007 at 10:34 PM.
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #2
    New Coder
    Join Date
    Jun 2005
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 2 Posts

  • Users who have thanked kiwibrit for this post:

    Troy297 (08-27-2007)

  • #3
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
        $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!
    Last edited by Troy297; 08-27-2007 at 02:35 AM.
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    PHP Code:
    $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.

  • Users who have thanked Inigoesdr for this post:

    Troy297 (08-27-2007)

  • #5
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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?

    Code:
    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....?
    Last edited by Troy297; 08-27-2007 at 04:30 AM.
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •