2

i'm using jquery countdown with php. i have given an end date which is going to the countdown. my problem is lets suppose 1 hour left is showing in countdown but when a user change its system time the countdown changes. like if a user back his time 1 hour then the counter will display the 2 hours left. is there any way to get the server time for more accurate time not the user system time. please help.

how can i get server time not user system time?

below is my jquery code

if($(pluginsArray[6]).length){

            $(pluginsArray[6]).each(function(){

                var $this = $(this),
                    dateObj = $this.data();

                var finalDate = new Date(dateObj.year, dateObj.month, dateObj.day, dateObj.hours, dateObj.minutes);

                $this.countdown({
                    timezone: +4, 
                    until : finalDate,
                    expiryText: '<div class="over">Closed.</div>',
                    onExpiry : function(){
                        setTimeout(function( ) { location.reload(); }, 5000);

                    },
                    format :'DHMS',
                    layout : '<b>{dn}</b> <span class="fs_medium d_inline_b m_right_5">days</span> <b>{hn}</b> <span class="fs_medium d_inline_b m_right_5">hrs</span> <b>{mn}</b> <span class="fs_medium d_inline_b m_right_5">min</span> <b>{sn}</b> <span class="fs_medium">sec</span>'
                });

            });


        }

and here is what i did in php

<div class="countdown color_redc d_inline_m fs_large second_font lh_small f_xs_20" style="font-size:26px;" data-year="<?= $aDate[0] ?>" data-month="<?= ($aDate[1] - 1) ?>" data-day="<?= $aDate[2] ?>" data-hours="<?= $aDate[3] ?>" data-minutes="<?= $aDate[4] ?>"></div>
Junaid Tariq
  • 231
  • 1
  • 2
  • 7
  • 1
    What you have tried so far? Please share your code – TheCodingFrog Jun 28 '15 at 08:19
  • Well you could make an ajax request to query the server time, but what for? It is the users issue if he changes his time. That should definitely not alter the outcome of whatever you try to implement, since all requests obviously have to be validated on the server side upon whatever happens when the countdown runs out. – arkascha Jun 28 '15 at 08:27
  • Apart from that: changing the logic does not make sense. If the user wants to manipulate on the client side he is free to do so. It also means he can alter your javascript code. You cannot protect from that somehow. But as said: your service has to be that robust, that this does not matter... – arkascha Jun 28 '15 at 08:28
  • you can see my code above. – Junaid Tariq Jun 28 '15 at 09:12

1 Answers1

0

Solution without PHP

What you can do, without coding any server side is using a public API to get current time.

Found a similar topic on StackoverFlow : Free Rest API to get current time as string (timezone irrelevant)

TimezoneDb provides a free API: http://timezonedb.com/api

GenoNames also has a RESTful API available to get the current time for a given location: http://www.geonames.org/export/ws-overview.html.

You can use Greenwich, UK if you'd like GMT.

GenoNames looks to be US only, TimezoneDb works you just need to register for a free public key.

Few people recommend timeapi.org but looks like they do not accept CROSS-DOMAIN request in Ajax, and the exemple they provide is no longer available.

Solution with PHP and jQuery Countdown configuration

Also you can ask jQuery CountDown to synchronyze with your server using serverSync option

$(selector).countdown({ 
    until:liftoffTime, serverSync: serverTime}); 

function serverTime() { 
    var time = null; 
    $.ajax({url: 'http://myserver.com/serverTime.php', 
        async: false, dataType: 'text', 
        success: function(text) { 
            time = new Date(text); 
        }, error: function(http, message, exc) { 
            time = new Date(); 
    }}); 
    return time; 
}

PHP file : serverTime.php

<?php 
$now = new DateTime(); 
echo $now->format("M j, Y H:i:s O")."\n"; 
?>

BUT

Keep in mind your user will always be able to change your code and fake it ... so if you need to implement some security this is not enough and you will need to code some backend stuff.

Community
  • 1
  • 1
sebastienbarbier
  • 6,542
  • 3
  • 29
  • 55
  • i appreciate your answer. my final date is coming from server but problem is still there i don't want let user fake it. if user changes his system time this should now effect the countdown at all. have you ever see timer on ebay. they have on their bidding products if you change your system time their countdown won't effect . – Junaid Tariq Jun 28 '15 at 09:52
  • There is absolutely no reason why user system time should affect your script if date come from PHP and no Javascript is used ... seems like we are missing something here. – sebastienbarbier Jun 28 '15 at 09:56
  • This might come from countdown JS plugin. May be try to define `serverSync` options when you create your object : http://keith-wood.name/countdownRef.html. I updated my answer. BUT keep in mind this is just a visual element, you should never activate key features (like sell on eBay) with a JS element, always protect your backend :D – sebastienbarbier Jun 28 '15 at 09:59