Note that there are some explanatory texts on larger screens.

plurals
  1. POif else in javascript counter timer
    primarykey
    data
    text
    <p>Im using a small script to display the amount of time since a given datetime value (passed from PHP query).</p> <p>The script is as follows:</p> <pre><code>&lt;script language="JavaScript"&gt; TargetDate = "&lt;?php echo $dti3; ?&gt;"; FinishMessage = "done" BackColor = "white"; ForeColor = "black"; CountActive = true; LeadingZero = true; DisplayFormat = "%%D%% Days %%H%%:%%M%%:%%S%% "; &lt;/script&gt; &lt;script type="text/javascript" language="JavaScript" src="time.js"&gt;&lt;/script&gt; </code></pre> <p>and time.js is</p> <pre><code>function calcage(secs, num1, num2) { s = ((Math.floor(secs/num1))%num2).toString(); if (LeadingZero &amp;&amp; s.length &lt; 2) s = "0" + s; return s; } function CountBack(secs) { if (secs &lt; 0) { document.getElementById("cntdwn").innerHTML = FinishMessage; return; } 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)); document.getElementById("cntdwn").innerHTML = DisplayStr; if (CountActive) setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod); } function putspan(backcolor, forecolor) { document.write("&lt;span id='cntdwn'&gt;&lt;/span&gt;"); } if (typeof(BackColor)=="undefined") BackColor = "white"; if (typeof(ForeColor)=="undefined") ForeColor= "#2A8827"; if (typeof(TargetDate)=="undefined") TargetDate = "12/31/2020 05:00:00"; if (typeof(DisplayFormat)=="undefined") DisplayFormat = "%%D%% days, %%H%% hours, %%M%% minutes, %%S%% seconds."; if (typeof(CountActive)=="undefined") CountActive = true; if (typeof(FinishMessage)=="undefined") FinishMessage = "no data"; if (typeof(CountStepper)!="number") CountStepper = +1; if (typeof(LeadingZero)=="undefined") LeadingZero = true; CountStepper = Math.ceil(CountStepper); if (CountStepper == 0) CountActive = false; var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 1000; putspan(BackColor, ForeColor); var dthen = new Date(TargetDate); var dnow = new Date(); if(CountStepper&gt;0) ddiff = new Date(dnow-dthen); else ddiff = new Date(dthen-dnow); gsecs = Math.floor(ddiff.valueOf()/1000); CountBack(gsecs); </code></pre> <p>I want to display the time since $dti3 as %%H%%:%%M%%:%%S%% if this is less than 24 hours, otherwise display just the number of days with some text. Ie:</p> <p>23 hours since date displays as : "23:00:00" but 25 hours displays as: "more than one day" and 49 hours displays as "more than two days" etc...</p> <p>I imagine this will require an if else statement somewhere but im not much of a JS coder (yet - im working on it!) so im not sure the best way to achieve this.</p> <p>Any help would be much appreciated,</p> <p>Many thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload