throny
throny

Reputation: 461

FlipClock - Use servertime instead of clienttime

I'm trying to get a eventmonitor on 7 different Raspberry Pis. They're all getting the same webpage with a FlipClock loaded.

My problem is: the time on the RasPis differs. (They can't get the actual ptb-time, because they are not allowed to connect to the internet)

This is how I called the Flipclock:

  <script type="text/javascript">
var clock = $('.clock').FlipClock({
  clockFace: 'TwentyFourHourClock',
  showSeconds: false
});

I thought I could solve the problem, if I get the actual date via PHP and set it while loading the FlipClock like this:

clock.setTime($servertime);

But I can't get this to work..

I'm quite new to this, so yeah, if anyone knows, how to solve it, please help me :(

EDIT: This is how my new call looks like:

     <div class="clock"></div>
  <script type="text/javascript">
   var serverTime = <?= time() ?>;
   var timeDifference = new Date - serverTime;
   var clock = $('.clock').FlipClock(timeDifference,{
      clockFace: 'TwentyFourHourClock',
      showSeconds: false
    });
  </script>

But this keeps my clock going strange, every refresh it takes a different hour and minute.

Upvotes: 3

Views: 612

Answers (2)

Louys Patrice Bessette
Louys Patrice Bessette

Reputation: 33943

This thread helped me... But I finally managed to do it another way, which I think is simplier:

var serverTime = '<?php echo Date('H:i:s'); ?>';
var clock = $('#clock').FlipClock(serverTime,{
  clockFace: 'TwentyFourHourClock',
});

Upvotes: 1

Mijago
Mijago

Reputation: 1639

Javascript is ment to be executed on the Client (Browser) only, so it can not get the server time. So, you have to call the Server to get the Date. One way is using Ajax and calling a simple script that sends only the servertime back.

Another way is implementing the Servertime directly inside the Script. If you use PHP, you can send the Time-Difference with something like that to the Script:

<script>
   var serverTime = <?= time() ?>
   var timeDifference = new Date - serverTime;
</script>

An different way is setting up a NTP Server on one of the Raspberrys, so all others can get the time from him at boot time. Here is your solution: http://raspberrypi.tomasgreno.cz/ntp-client-and-server.html

Have fun!

Upvotes: 3

Related Questions