Page 1 of 2

MinutesSinceLastRainTip

Posted: Fri 21 Feb 2014 5:53 am
by Alan
Hi to all,
I am hoping that somebody with javascript experience can help me out with this one as my knowledge is next to zero.
In the latest Cumulus version (1.9.4) Steve made available a tag to return the number of minutes since the last rain tip (<#MinutesSinceLastRainTip>). This for me is really useful but not in its current format. I would really like to convert the returned number of minutes into xxDays : xxHrs : xxMins
I have tried various samples that were retrieved from the net but all to no avail.... I am now at my wits end. Any help with this would be greatly appreciated.
The applicable code that I am trying to modify is as follows.....

<table class="data" width="1900px" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td width="100%"align="center"valign="center"style="font-size:600%"style="color:blue"><#MinutesSinceLastRainTip></td>
</tr>
</table>
</body>
</html>

Thanking any helpers in advance
Cheers!
Alan

Re: MinutesSinceLastRainTip

Posted: Fri 21 Feb 2014 6:58 am
by milos.jirik
Hi Alan,

this can be replaced by the script as written below.

Code: Select all

<?php
       # script for converting time since last precipitation
        $minutes = $MinutesSinceLastRainTip;                 // variable definition
        // determine the variables calculated from $minutes    
        $d = floor ($minutes / 1440);
        $h = floor (($minutes - $d * 1440) / 60);
        $m = $minutes - ($d * 1440) - ($h * 60);
        // output values
        if($d == 0){
        echo "{$h}&nbsp;h.&nbsp;{$m}&nbsp;min.";
          }
          else {
          if($d == 1){
           echo "{$d}&nbsp;Day&nbsp;{$h}&nbsp;Hrs&nbsp;{$m}&nbsp;Mins";
              } 
          else {
          if($d >= 2){
          echo "{$d}&nbsp;Days&nbsp;{$h}&nbsp;Hrs&nbsp;{$m}&nbsp;Mins";
          }
         }
        }    
     #  end of script
?>
Cheers!

Milos

Re: MinutesSinceLastRainTip

Posted: Fri 21 Feb 2014 8:06 am
by steve
With JavaScript, you could do it as follows. There may be more elegant ways!

Code: Select all

<td width="100%"align="center"valign="center"style="font-size:600%"style="color:blue"><script type="text/javascript">document.write(Math.floor(<#MinutesSinceLastRainTip>/24/60) + 'Days : ' + Math.floor(<#MinutesSinceLastRainTip>/60%24) + 'Hrs : ' + Math.floor(<#MinutesSinceLastRainTip>%60) + 'Mins');</script></td>

Re: MinutesSinceLastRainTip

Posted: Fri 21 Feb 2014 9:15 am
by milos.jirik
Hi Steve,

thanks for converting my PHP script to Javascript. I'm not that familiar with Javascript.

Best regards

Milos

Re: MinutesSinceLastRainTip

Posted: Fri 21 Feb 2014 9:48 am
by Alan
Hi to Steve and Milos,
Thanks to both of you for your efforts, the javascript supplied works beautifully.
Cheers and best regards,
Alan

Re: MinutesSinceLastRainTip

Posted: Sat 22 Feb 2014 9:21 am
by ace2
very nice......YONK


sorry, I've stolen that code and used it in my page.

hope that's ok??? :)



http://www.users.on.net/~ace2/index.htm

Re: MinutesSinceLastRainTip

Posted: Sat 22 Feb 2014 11:40 pm
by captzero
If any value is equal to '1' this php code will write 'days', 'hours' or 'minutes' as the singular, i.e...day...hr...min.

Code: Select all

$minutes = '<#MinutesSinceLastRainTip>';       
	   
$d = floor ($minutes / 1440);
$h = floor (($minutes - $d * 1440) / 60);
$m = $minutes - ($d * 1440) - ($h * 60);

$day ='day';
$days ='days';
$hour ='hr';
$hours ='hrs';
$min = 'min';
$mins = 'mins';

if ($d == '1') echo "{$d}&nbsp;{$day}&nbsp;"; else echo "{$d}&nbsp;{$days}&nbsp;";
if ($h == '1') echo "{$h}&nbsp;{$hour}&nbsp;"; else echo "{$h}&nbsp;{$hours}&nbsp;";
if ($m == '1') echo "{$m}&nbsp;{$min}&nbsp;"; else echo "{$m}&nbsp;{$mins}";

Re: MinutesSinceLastRainTip

Posted: Sun 23 Feb 2014 3:59 am
by Alan
Dan, thank you very much for your suggestion. However, as I mentioned in my original post, I know next to nothing about jvascript and must add that I know absolutely nothing about PHP, I have never used it. So would you be so kind so as to show me exactly where your supplied code would be embedded into the code that I supplied. I was aware of the use of plural nomenclature only but was prepared to put up with. This solution of yours though seems to be the bees knees.

Cheers! and thanks,
Alan

Re: MinutesSinceLastRainTip

Posted: Sun 23 Feb 2014 5:24 am
by BCJKiwi
Or this;
To insert this into the standard html wherever you want it displayed. e.g into a new table row in say index.htm. You will need to add into the ....T.htm file in C:\cumulus\web as Cumulus needs to fill the <#MinutesSinceLastRainTip> tag.

Code: Select all

  <tr>
    <td colspan="7">
      <b>Age of Last rain Tip: </b>
      <?php
         $minutes = '<#MinutesSinceLastRainTip>';

         $d = floor ($minutes / 1440);
         $h = floor (($minutes - $d * 1440) / 60);
         $m = $minutes - ($d * 1440) - ($h * 60);

         $dy = ($d === 1)? $d . " Day:" : $d . " Days:";
         $hr = ($h === 1)? $h . " Hr:" :  $h . " Hrs:";
         $mi = ($m === 1)? $d . " Min:" : $m . " Mins";
         echo "$dy $hr $mi";
      ?>
    </td>
  </tr>
The <?php ?> are the php tags as this is a bit of php code.
The ( )? is the if part of an "if else" statement.
The : in front of the $d is the else part.

Re: MinutesSinceLastRainTip

Posted: Sun 23 Feb 2014 6:21 am
by captzero
Hi Alan,

I should have read your initial post more carefully.
You did ask for a javascript solution and I posted a PHP solution and it wont work on your webpage as it stands. I'm afraid I have bugger all knowledge of javascript so I'll have to leave that and hope that someone else on the forum can provide something for you.

Re: MinutesSinceLastRainTip

Posted: Sun 23 Feb 2014 6:36 am
by ace2
<table class="data" width="1900px" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td width="100%"align="center"valign="center"style="font-
size:600%"style="color:blue"><script type="text/
javascript">document.write(Math.floor
(<#MinutesSinceLastRainTip>/24/60) + 'Days : ' + Math.floor
(<#MinutesSinceLastRainTip>/60%24) + 'Hrs : ' + Math.floor
(<#MinutesSinceLastRainTip>%60) + 'Mins');</script></td>
</tr>
</table>
</body>
</html>

or anywhere you want!
just remember it must be in a template file to be processed to work.
i added mine into indexT.htm below the forecast


http://www.users.on.net/~ace2/index.htm
the output file, I edited the code a little to suit my needs.

Re: MinutesSinceLastRainTip

Posted: Sun 23 Feb 2014 9:28 am
by steve
You can amend that JavaScript I provided to do the same kind of thing (i.e. do the plural check). You would probably want to create a separate function rather than putting it 'inline', as it's going to get messy. I'll see if I can come up with something later, if none of the real JavaScript experts have chipped in by then.

Re: MinutesSinceLastRainTip

Posted: Sun 23 Feb 2014 9:46 am
by mcrossley

Code: Select all

<script type="text/javascript">
var days = Math.floor(<#MinutesSinceLastRainTip>/24/60);
var hrs = Math.floor(<#MinutesSinceLastRainTip>/60%24);
var mins = Math.floor(<#MinutesSinceLastRainTip>%60);
function plur(x){return x > 1 ? 's' : ''};
document.write(days + 'Day' + plur(days) + ' : ' + hrs + 'Hr' + plur(hrs) + ' : ' + mins + 'Min' + plur(mins));
</script>
[/s]

Nowt wrong with your js Steve.

Do you have any idea how hard it is to peck out code on a tablet!

Re: MinutesSinceLastRainTip

Posted: Sun 23 Feb 2014 9:47 am
by steve
Thanks, Mark, that's much more elegant than my effort would have been!

Re: MinutesSinceLastRainTip

Posted: Sun 23 Feb 2014 9:56 am
by Alan
Steve, even with my limited knowledge I can understand what you mean by it getting messy (because I have given this point consideration). What I find thoroughly encouraging is the involvement of various members on this topic.
Alan