25 Replies Latest reply on Jan 24, 2011 9:31 PM by kglad

    Server time

    Ron Colmen Level 2

      I'm trying to create a clock which shows the time returned by the server. Help me to get this code working with the server time on..

      (E.g.: time.php returns 1295532770)

       

      server = new LoadVars();

      server.onData = function (src){

      clock_txt.onEnterFrame = time;

      this.time = Number(src);

      };

      server.load("http://www.me.com/time.php");

       

      var seconds = this.time/1000

      var minutes = this.time/60

      var hours = this.time/60

      if (hours<12) {

      ampm = "AM";

      }

      else{

      ampm = "PM";

      }

      while(hours >12){

      hours = hours - 12;

      }

      if(hours<10)

      {

      hours = "0" + hours;

      }

      if(minutes<10)

      {

      minutes = "0" + minutes;

      }

      if(seconds<10)

         {

      seconds = "0" + seconds;

      }

      clock_txt.text = hours + ":" + minutes + ":" + seconds +" "+ ampm;

        • 1. Re: Server time
          kglad Adobe Community Professional & MVP

          you have some problems with logic but to start, there's nothing returned from the php file.  use the trace function or a textfield to confirm.

          • 2. Re: Server time
            Ron Colmen Level 2

            Thanks.

             

            server = new LoadVars();

            server.onData = function (src){

            clock_txt.onEnterFrame = time;

            this.time = Number(src);

            trace (this.time);

            };

             

            outputs

            1295554175

            1295554175

            1295554176

            1295554176

            1295554176

            1295554176

            ....

            • 3. Re: Server time
              kglad Adobe Community Professional & MVP

              then use:

               

               

              var tl:MovieClip=this;

              var startTime:Number;

               

              server = new LoadVars();

              server.onData = function (src){

              tl.timeMS = Number(src);

              startTime=getTimer();

              clock_txt.onEnterFrame = timeF;

              };

              server.load("http://www.me.com/time.php");

               

              function timeF(){

               

              var d:Date = new Date(tl.timeMS+getTimer()-startTime);
              if(d.getHours()>12){
              var hours:Number=d.getHours()-12;
              var ampm:String = "pm";
              } else if(d.getHours()==12){
              hours=12;
              ampm="pm"
              } else {
              hours=d.getHours();
              ampm="am";
              }

              var hoursS:String = formatS(hours.toString());
              var minutes:String = formatS(d.getMinutes().toString());
              var seconds:String = formatS(d.getSeconds().toString());

              clock_txt.text = hours + ":" + minutes + ":" + seconds +" "+ ampm;

              }

               

              function formatS(s:String):String{

              while(s.length<2){

              s="0"+s;

              }

              return s;

              }

              1 person found this helpful
              • 4. Re: Server time
                Ron Colmen Level 2

                Thanks Kglad but nothing seems to appear.

                • 5. Re: Server time
                  kglad Adobe Community Professional & MVP

                  then you don't have a textfield with visible text or you onData() method isn't being called.  use the trace() function or a textfield to see which it is.

                  • 6. Re: Server time
                    Ron Colmen Level 2

                    server = new LoadVars();

                    server.onData = function (src){

                    tl.timeMS = Number(src);

                    startTime=getTimer();

                    clock_txt.onEnterFrame = timeF;

                    trace(tl.timeMS);

                    };

                    Outputs

                    1295568130

                     

                    Also as you suggested I inserted a new dynamic text box with an instance name of clock_txt. But nothing is displayed.

                    • 7. Re: Server time
                      kglad Adobe Community Professional & MVP

                      enable the border of your textfield.  can you see it when you test?  if not, you need to embed your font, check visible, check alpha.  if yes, check that the font color is different from the background and there's no textfield restriction.

                      • 8. Re: Server time
                        Ron Colmen Level 2
                        enable the border of your textfield.  can you see it when you test?  - YES
                        ...if yes, check that the font color is different from the background - YES IT IS.
                        ... and there's no textfield restriction - NO.

                         

                         

                        Strange. I wonder if there's something wrong in the code then?

                         

                        var tl:MovieClip=this;

                        var startTime:Number;

                         

                        server = new LoadVars();

                        server.onData = function (src){

                        tl.timeMS = Number(src);

                        startTime=getTimer();

                        clock_txt.onEnterFrame = timeF;

                        trace(tl.timeMS);

                        };

                        server.load("http://www.me.com/time.php");

                         

                        function timeF(){

                         

                        var d:Date = new Date(tl.timeMS+getTimer()-startTime);

                        if(d.getHours()>12){

                        var hours:Number=d.getHours()-12;

                        var ampm:String = "pm";

                        } else if(d.getHours()==12){

                        hours=12;

                        ampm="pm"

                        } else {

                        hours=d.getHours();

                        ampm="am";

                        }

                         

                        var hoursS:String = formatS(hours.toString());

                        var minutes:String = formatS(d.getMinutes().toString());

                        var seconds:String = formatS(d.getSeconds().toString());

                         

                        clock_txt.text = hoursS + ":" + minutes + ":" + seconds +" "+ ampm;

                        }

                         

                        function formatS(s:String):String{

                        while(s.length<2){

                        s="0"+s;

                        }

                        return s;

                        }

                        • 9. Re: Server time
                          kglad Adobe Community Professional & MVP

                          if something were wrong with the code you'd still see something in the textfield even if it's undefined:undefined:undefined:undefined

                           

                          but you can always test with:

                           

                           

                          clock_txt.text = "this is a test";

                          • 10. Re: Server time
                            Ron Colmen Level 2

                            clock_txt.text = "this is a test"; shows "this is a test";

                             

                            But it's empty for the clock code. also when I did a trace for the following all those were empty.

                            trace (hours);

                            trace (minutes);

                            trace (seconds);

                            trace (ampm);

                            trace (d);

                            • 11. Re: Server time
                              kglad Adobe Community Professional & MVP

                              are you testing locally?  do you get a return from that php when testing locally?  if yes and yes, copy the first several lines of your output panel after using:

                               


                               

                               

                              var tl:MovieClip=this;

                              var startTime:Number;

                               

                              server = new LoadVars();

                              server.onData = function (src){

                              tl.timeMS = Number(src);

                              startTime=getTimer();

                              clock_txt.onEnterFrame = timeF;

                               

                              };

                              server.load("http://www.me.com/time.php");

                               

                              function timeF(){

                               

                              trace("timeMS "+tl.timeMS);

                              var d:Date = new Date(tl.timeMS+getTimer()-startTime);

                              if(d.getHours()>12){

                              var hours:Number=d.getHours()-12;

                              var ampm:String = "pm";

                              } else if(d.getHours()==12){

                              hours=12;

                              ampm="pm"

                              } else {

                              hours=d.getHours();

                              ampm="am";

                              }

                              trace("hours "+hours);

                              var hoursS:String = formatS(hours.toString());

                              var minutes:String = formatS(d.getMinutes().toString());

                              var seconds:String = formatS(d.getSeconds().toString());

                              trace("hourS "+hourS);

                              clock_txt.text = hoursS + ":" + minutes + ":" + seconds +" "+ ampm;

                              trace(clock_txt+" "+clock_txt.text);

                              }

                               

                              function formatS(s:String):String{

                              while(s.length<2){

                              s="0"+s;

                              }

                              return s;

                              }

                              • 12. Re: Server time
                                Ron Colmen Level 2

                                With that code nothing is been output.

                                 

                                but when I did this

                                 

                                server = new LoadVars();

                                server.onData = function (src){

                                tl.timeMS = Number(src);

                                startTime=getTimer();

                                clock_txt.onEnterFrame = timeF;

                                trace("timeMS "+tl.timeMS);

                                };

                                 

                                the OUTPUT was

                                timeMS 1295689620

                                • 13. Re: Server time
                                  kglad Adobe Community Professional & MVP

                                  clock_txt is not a movieclip.  an onEnterFrame method can only be applied to a movieclip.  fix that.


                                  1 person found this helpful
                                  • 14. Re: Server time
                                    Ron Colmen Level 2

                                    oh ok. But still the time doesn't show up.

                                     

                                    function timeF(){

                                    trace("timeMS "+tl.timeMS);

                                    var d:Date = new Date(tl.timeMS+getTimer()-startTime);

                                     

                                    OUTPUT

                                    timeMS 1295694898

                                    hours 5

                                    hourS undefined

                                    _level0.clock_txt 05:24:54 am

                                    timeMS 1295694898

                                    hours 5

                                    hourS undefined

                                    _level0.clock_txt 05:24:54 am

                                    timeMS 1295694898

                                    hours 5

                                    hourS undefined

                                    _level0.clock_txt 05:24:55 am

                                    timeMS 1295694898

                                    hours 5

                                    hourS undefined

                                    _level0.clock_txt 05:24:55 am

                                    timeMS 1295694898

                                    hours 5

                                    hourS undefined

                                    _level0.clock_txt 05:24:55 am

                                    • 15. Re: Server time
                                      kglad Adobe Community Professional & MVP

                                      remove all the trace() statements.

                                       

                                      the code is working as expected.  clock_txt.text is correct.

                                      • 16. Re: Server time
                                        Ron Colmen Level 2

                                        oh! it works!!!

                                         

                                        Thank you very much Kglad.

                                        • 17. Re: Server time
                                          kglad Adobe Community Professional & MVP

                                          you're welcome.


                                          • 18. Re: Server time
                                            Ron Colmen Level 2

                                            There's something strange happening. Each time I open the swf the time starts from 5:28:01am? Why isn't it displaying the time returned by the server?

                                            • 19. Re: Server time
                                              kglad Adobe Community Professional & MVP

                                              use the trace() function to see if the number returned from the server changes.


                                              • 20. Re: Server time
                                                Ron Colmen Level 2

                                                yes, each time the number returned from the php changes to a higher value. but all ways the time starts at 05:29:11am

                                                 

                                                 

                                                server = new LoadVars();

                                                server.onData = function (src){

                                                tl.timeMS = Number(src);

                                                startTime=getTimer();

                                                clock_txt.onEnterFrame = timeF;

                                                trace (tl.timeMS);

                                                };

                                                 

                                                OUTPUT E.g.

                                                1295951360

                                                 

                                                var tl:MovieClip=this;

                                                var startTime:Number;

                                                 

                                                server = new LoadVars();

                                                server.onData = function (src){

                                                tl.timeMS = Number(src);

                                                startTime=getTimer();

                                                clock_txt.onEnterFrame = timeF;

                                                trace (tl.timeMS);

                                                };

                                                server.load("http://www.myweb.com/time.php");

                                                 

                                                function timeF(){

                                                var d:Date = new Date(tl.timeMS+getTimer()-startTime);

                                                if(d.getHours()>12){

                                                var hours:Number=d.getHours()-12;

                                                var ampm:String = "pm";

                                                } else if(d.getHours()==12){

                                                hours=12;

                                                ampm="pm"

                                                } else {

                                                hours=d.getHours();

                                                ampm="am";

                                                }

                                                var hoursS:String = formatS(hours.toString());

                                                var minutes:String = formatS(d.getMinutes().toString());

                                                var seconds:String = formatS(d.getSeconds().toString());

                                                _root.clock_txt.clock_txt.text = hoursS + ":" + minutes + ":" + seconds +" "+ ampm;

                                                }

                                                 

                                                function formatS(s:String):String{

                                                while(s.length<2){

                                                s="0"+s;

                                                }

                                                return s;

                                                }

                                                • 21. Re: Server time
                                                  kglad Adobe Community Professional & MVP

                                                  what exactly is returned by time.php?

                                                  • 22. Re: Server time
                                                    Ron Colmen Level 2

                                                    the time in miliseconds.

                                                     

                                                    <?php

                                                    $tz =  new DateTimeZone('Europe/Warsaw');

                                                    $date = new DateTime('now', $tz);

                                                    $gettime = strtotime($date->format('Y-m-d H:i:s'));

                                                    echo ($gettime);

                                                    ?>

                                                    • 23. Re: Server time
                                                      kglad Adobe Community Professional & MVP

                                                      it looks more like it's returning seconds, not ms.   try:

                                                       

                                                      var tl:MovieClip=this;

                                                      var startTime:Number;

                                                       

                                                      server = new LoadVars();

                                                      server.onData = function (src){

                                                      tl.timeMS = Number(src);

                                                      startTime=getTimer();

                                                      clock_txt.onEnterFrame = timeF;

                                                      trace (tl.timeMS);

                                                      };

                                                      server.load("http://www.myweb.com/time.php");

                                                       

                                                      function timeF(){

                                                      var d:Date = new Date(tl.timeMS*1000+getTimer()-startTime);

                                                      if(d.getHours()>12){

                                                      var hours:Number=d.getHours()-12;

                                                      var ampm:String = "pm";

                                                      } else if(d.getHours()==12){

                                                      hours=12;

                                                      ampm="pm"

                                                      } else {

                                                      hours=d.getHours();

                                                      ampm="am";

                                                      }

                                                      var hoursS:String = formatS(hours.toString());

                                                      var minutes:String = formatS(d.getMinutes().toString());

                                                      var seconds:String = formatS(d.getSeconds().toString());

                                                      _root.clock_txt.clock_txt.text = hoursS + ":" + minutes + ":" + seconds +" "+ ampm;

                                                      }

                                                       

                                                      function formatS(s:String):String{

                                                      while(s.length<2){

                                                      s="0"+s;

                                                      }

                                                      return s;

                                                      }

                                                      • 24. Re: Server time
                                                        Ron Colmen Level 2

                                                        Thanks Kglad!

                                                        • 25. Re: Server time
                                                          kglad Adobe Community Professional & MVP

                                                          you're welcome.