Server time actionscript
A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: Server time actionscript

  1. #1
    Junior Member
    Join Date
    Dec 2005
    Posts
    12

    Server time actionscript

    Hi all,

    I am trying to create a world clock in Actionscript, which will be used in different time zones at the same time. To be able to do this, I would like to use the server time as a "constant" factor, and add or deduct the time difference for every time zone.

    Well here is the question... is it possible to read server time from Actionscript?

    Thanx in advance,
    courdi

  2. #2
    Senior Member flamedude's Avatar
    Join Date
    Jun 2003
    Location
    Vancouver, BC
    Posts
    252
    I am sure you could get some PHP or ASP to feed the time on the server to your flash project. But I am not sure how you would do this....

    But thats where I would start.

  3. #3
    Junior Member
    Join Date
    Dec 2005
    Posts
    12
    Thanx flamedude for your reply, I will cetanly look into APS for this.
    Another thing, is it possible to read Internet time from some time server, or is that too far feched?

    courdi

  4. #4
    Senior Member ellings's Avatar
    Join Date
    Sep 2001
    Posts
    155
    I you find a time server providing this in a easy workable format it should be easy. I could use something like this my self so I'll se if I can find something usable. Please let me know if you find anything.

    /Per-Henrik

  5. #5
    Junior Member
    Join Date
    Dec 2005
    Posts
    12
    I have not been able to find a time server that I could connect to via Actionscript yet... but I have looked at the APS side and was able to find a script that sends back the server time:


    Code:
    <%
    'Dimension variables
    Dim strMessage
    
    'Write the server time on the web page using the VBScript Time() function
    
    Response.Write ("The time on the server is: " & Time())
    
    'Close the server script
    %>
    As I am no databse vizard, I will have too look into passing the time from ASP (response.Write ()) to Flash Actionscript... If anyone knows the way to do this, please help.

    Cheers,
    courdi
    Last edited by courdi; 12-07-2005 at 05:45 AM.

  6. #6
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560
    You don't actually need the servertime for your purpose.

    The Date class has a property which gives you the offset to universal time:
    Code:
    var d:Date = new Date ();
    trace(d.getTimezoneOffset());
    But in your last post you wrote something about a "time server",
    are you talking about getting the time via ntp from a time server,
    or was that just a typo?
    My letters on the F1 key have faded, how are yours today?

  7. #7
    Junior Member
    Join Date
    Dec 2005
    Posts
    12
    Hi McUsher,

    Thanx for your reply. I am not familiar with getTimezoneOffset() property, but will certainly look into it. In a quick test I just did with it, it returns -60 (?)...

    As I mentioned before, connecting to a time server (via ntp as you mentioned) may be "a bit" far fetched, but would be pretty interesting...

    cheers,
    courdi

  8. #8
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560
    Ok two things:
    1) Place your caret within the word "getTimeOffset" in Flash and hit F1, and you don't need to ask what -60 means...
    2) You can't push information from server to flash, flash needs to ask for it:
    server/getTime.asp:
    Code:
    <%
    Response.Write ("<time>" & Time() & "</time>")
    %>
    in Flash:
    Code:
    var xml:XML=new XML();
    xml.ignoreWhite = true;
    xml.onLoad = timeReceived;
    // getTime.asp and html/swf must be in same dir
    xml.load("getTime.asp");
    //
    function timeReceived (success:Boolean) {
    	if(success){
    		trace("timeReceived: "+this.firstChild.firstChild.nodeValue);
    	} else {
    		trace("can't get 'getTime.asp");
    	}
    }
    But this is only necessary, if you really wanna connect to a ntp time server in asp .. Then u could compare Client Time to ntp server time ... with the drawback of latency not counting in ...
    My letters on the F1 key have faded, how are yours today?

  9. #9
    Junior Member
    Join Date
    Dec 2005
    Posts
    12
    Thank you for you help McUsher,

    Ok, let me see if I get this... I have a ASP file getTime.asp on the server with the above code. In the same directory I have put a SWF with the code you mentioned...

    When I call the getTime.asp in the browser, it shows me the time on the server... no probs here. The SWF file on the other hand traces "timeRecieved: null"... hmmm.

    P.S. My F1 key looks pale today
    Last edited by courdi; 12-07-2005 at 06:17 AM. Reason: typo

  10. #10
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560
    hehe, thumbs up to your P.S.

    this trace: timeReceived: null says, the xml parsing doesn't return the expected..
    add a line "trace(this)" for the whole answer of the asp script.

    Other ppl prefer the LoadVars, but as i prefer the xml object,
    which only is on first sight more complicated i put that in ...
    My letters on the F1 key have faded, how are yours today?

  11. #11
    Junior Member
    Join Date
    Dec 2005
    Posts
    12
    Thanx agina McUsher, you rule!

    I have finally made the world clock with the getTimezoneOffset() method as you suggested and I must say it works like a charm.

    Here is the code I used, so others don't need to bother :

    Code:
    // define time offset with Greenwich time
    var d:Date = new Date ();
    _root.greenwich_time = d.getTimezoneOffset()/(-60);
    
    // define time variables
    function howlong(arg) {
    	if (length(arg)==1) {
    		arg = "0" + arg;
    		return arg;
    		}
    	else {
    		arg = arg;
    		return arg;
    		}
    	}
    myDate = new Date();
    
    hr = howlong(String(myDate.getHours()) - greenwich_time);
    mnt = howlong(String(myDate.getMinutes()));
    
    daytext = myDate.getDay();
    dd = myDate.getDate();
    mm = myDate.getMonth();
    yyyy = myDate.getFullYear();
    
    // define the date with the above variables
    local_date = (dd + "-" + (mm+1) + "-" + yyyy);
    
    // define the different time for the timezones
    amsterdam_time = ((hr+1) + ":" + mnt) 
    lisbon_time = ((hr) + ":" + mnt)
    istanbul_time = ((hr+2) + ":" + mnt)
    
    //define the day of the week
    switch (daytext) {
            case 0:	day_text = "Sunday";
            break;
            case 1:	day_text = "Monday";
            break;
            case 2:	day_text = "Tuesday";
            break;
            case 3:	day_text = "Wednesday";
            break;
            case 4:	day_text = "Thursday";
            break
            case 5:	day_text = "Friday";
            break;
            case 6:	day_text = "Saturday";
            break
    }
    Thanx again.

    Cheers,
    courdi

    P.S. Keep pressing that F1 key.
    Last edited by courdi; 12-07-2005 at 11:56 AM. Reason: typo

  12. #12
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560
    Hehe.
    Sometimes everyone needs a little push in the right direction.

    To your weekday naming, i think that switch/case is a bit too complicated,
    try the approach they did in Date.getDay() help section:
    var dayOfWeek_array:Array = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var today_date:Date = new Date();
    var day_str:String = dayOfWeek_array[today_date.getDay()];
    trace("Today is "+day_str);
    :evilgrin:
    My letters on the F1 key have faded, how are yours today?

  13. #13
    Junior Member
    Join Date
    Dec 2005
    Posts
    12
    Thanks again buddy for the Array tip... will try it out.

    cheers,
    courdi

  14. #14
    Junior Member
    Join Date
    Aug 2004
    Posts
    16
    Sorry for bumping an old thread, but how would I take Daylight-Savings into account?
    Would I need to make an if-Statement, that checks for each location, that has daylight-saving, if the current date falls into the daylight-savings time frame or is there an easier solution?

  15. #15
    Junior Member
    Join Date
    Aug 2004
    Posts
    16
    Nobody a better idea than an if-statement?

    I keep thinking there must be a better way, considering that the getTimezoneOffset()-Method can take daylight-savings into account. I'm just not experienced enough to figure it out.

Posting Permissions

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




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com