1

Here is a JavaScript that I use to generate some countdowns, my problem is that the JavaScript is using the date from the users PC, is there any way to modify the script to use something like :

<?php 
date_default_timezone_set('Ireland/Dublin');
$date = date('m/d/Y h:i:s A', time());?>

Here is the current code used on the site to display multiple countdown on the page.

   <script type="text/javascript" language="JavaScript">//<![CDATA[


<?php
    echo 'StartCountDown("c' . $post->ID . '","' . $enddate . '")';

    ?>


  function StartCountDown(myDiv,myTargetDate)
  {
    var dthen   = new Date(myTargetDate);
    var dnow    = new Date();
    ddiff       = new Date(dthen-dnow);
    gsecs       = Math.floor(ddiff.valueOf()/1000);
    CountBack(myDiv,gsecs);
  }

  function Calcage(secs, num1, num2)
  {
    s = ((Math.floor(secs/num1))%num2).toString();
    if (s.length < 2) 
    {   
      s = "0" + s;
    }
    return (s);
  }

  function CountBack(myDiv, secs)
  {
    var DisplayStr;
    var DisplayFormat = "%%D%% %%H%%:%%M%%:%%S%%";
    DisplayStr = DisplayFormat.replace(/%%D%%/g,    Calcage(secs,86400,100000));
    DisplayStr = DisplayStr.replace(/%%H%%/g,       Calcage(secs,3600,24));
    DisplayStr = DisplayStr.replace(/%%M%%/g,       Calcage(secs,60,60));
    DisplayStr = DisplayStr.replace(/%%S%%/g,       Calcage(secs,1,60));
    if(secs > 0)
    {   
      document.getElementById(myDiv).innerHTML = DisplayStr;
      setTimeout("CountBack('" + myDiv + "'," + (secs-1) + ");", 990);
    }
    else
    {
      document.getElementById(myDiv).innerHTML = "&nbsp;SOLD OUT";
    }
  }

//]]></script>

Any help will be much appreciated.

2 Answers 2

4

It looks like its as simple as changing

var dnow    = new Date();

to something like:

<?php date_default_timezone_set('Ireland/Dublin'); ?>
var dnow    = new Date( <?= time(); ?>);
Sign up to request clarification or add additional context in comments.

1 Comment

There is no date format - It is up to the JS to determine how it wants to output the date and what format it is done in. This solution uses time, which returns a UNIX timestamp to set the JS Date object to the server time.
0

You should take a look at this answer here on SO, the snippet written by @pimvdb is really good and really readable. Very straightforward to modify aswell.

1 Comment

no no really that easy, perhaps you can come with an example - I have no idea of Javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.