Setting the time zone when using jQuery CountDown

We currently have this code that works, except that it should use the specified time zone, not the user's time zone:

$('#countdown').countdown('<?php echo $date_end?>').on('update.countdown', function(event) { var format = '%-S second%!S'; if (event.offset.years > 0 || event.offset.weeks > 0 || event.offset.days > 0 || event.offset.hours > 0 || event.offset.minutes > 0) { if(event.offset.minutes > 0) { format = '%-M minute%!M ' + format; } if(event.offset.hours > 0) { format = '%-H hour%!H ' + format; } if(event.offset.days > 0) { format = '%-d day%!d ' + format; } if(event.offset.weeks > 0) { format = '%-w week%!w ' + format; } } $(this).html(event.strftime(format)); }).on('finish.countdown', function(event) { $(this).html('Finished'); }); 

I saw some examples of adding time zones, but no one uses the jquery countdown plugin like we do.

Any ideas on how to add +10 as your current timezone? why does he use this, not the user's timezone?

Thanks.

+6
source share
3 answers

I assume that you are using https://github.com/hilios/jQuery.countdown because it does not have the time zone settings to use it.

I used time.js and time-timezone-with-data.js (contains all time zones) to handle the time zone.

 <div data-countdown="2015/05/20" data-zone="US/Central" class="countdown"></div> 

to convert the date to the correct time zone and return in milliseconds

 var timefun = function (countdown,zone){ var date = moment.tz(countdown,"YYYY/MM/DD",zone); return date.valueOf(); } 

use the above function to pass milliseconds as an argument to jQuery.countdown ()

 $(".countdown").countdown(timefun($(".countdown").data("countdown"),$(".countdown").data("zone"))) 

Example: http://jsfiddle.net/heLrsnqx/2/

if you use http://keith-wood.name/countdown.html see the following example

Example: http://jsfiddle.net/w1oyv8kv/

+5
source

It seems that the actual date that is used in the code comes from this PHP snippet. You can set the default timezone in PHP using:

 date_default_timezone_set($timezone_identifier); 

$timezone_identifier is a string that can be like 'UTC' or 'America/Los_Angeles' .

See the related documents for a complete list of supported timezone identifiers.

+4
source

According to the documentation, you can access the finalDate object using event.finalDate . Therefore, if you cannot install the time zone server, do event.finalDate.setUTCHours(10) before calling event.strftime

Read more about setUTCHours here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCHours

+1
source

Source: https://habr.com/ru/post/986625/


All Articles