Page 1 of 3

Lightning notification

Posted: Thu 25 Jun 2020 7:17 pm
by Mapantz
I'm trying to get text on my website to flash when get a lightning strike. It does work, and then it doesn't.

I've tried to do it so that text will flash for 30 minutes after the last strike has been detected. I thought I had it solved, but it fails too often and i'm not sure why.

Here's what I get CMX to upload for the lightning flash stuff:

Code: Select all

$lightningtimenum = "<#LightningTime format="yyyMMddHHmmss">";
$currenttime= date('ymjhi');
$lightningalert=date('ymjhi', strtotime($lightningtimenum));
$lightningtimeago= $lightningalert - $currenttime;
Here's what outputs:

https://warehamwx.co.uk/gw1000.php?sce=view

I then used this on my webpage:

Code: Select all

<?php if ($lightningtimeago > -30) { echo '<span class="lightning_alert">&nbsp;&nbsp;Lightning Nearby</span> '; } else { echo '&nbsp;&nbsp;Lightning'; }?>
$lightningtimeago was originally showing -1 for 1 minutes, -2 for 2 minutes and so on. However, within 5 minutes of a strike being detected, $lightningtimeago then showed -50.

As it stands now, the last strike detected was 20 minutes ago, and $lightningtimeago shows -61.

I know the coding is a fudge, but I don't understand why $lightningtimeago is not being reset correctly?

Any help would be greatly appreciated. :oops:

Re: Lightning notification

Posted: Fri 26 Jun 2020 5:54 am
by sfws
Mapantz wrote: Thu 25 Jun 2020 7:17 pm $lightningtimeago= $lightningalert - $currenttime;
That is not the right way to subtract times!

Have you forgotten that there are 60 seconds in minute and 60 minutes in an hour, you are mixing base 60 and base 10. No wonder the result jumps when the minute changes!

Why not use something like

Code: Select all

$lightningtime = new DateTime( "<#LightningTime format="yyy-MM-dd HH:mm:ss">");
$currenttime= new DateTime(date());
$lightningtimeago= $lightningtime ->diff($currenttime);
if($lightningtimeago -> format("i") < 30)  /* convert to minutes,  runs beyond 60 I think */
{
    flash text action
}
There are other ways of coding this, and this might not be best, but it gives you an idea. You can look up the date_diff, date_sub etc. functions on php.net yourself to find out more.
I have not tested this exact code although I use a similar construct for checking whether today when reporting last rain time, you might need something a little more complicated in condition because I am unsure if the convert to minutes works for multiple days. (In general, these PHP functions work for short periods like days, but not for longer periods like months or years, as I commented, in the forum, back when we had the leap day, so this might fail when no lightning for months).

Edit: Corrected my typos, saying data when I meant date!

Re: Lightning notification

Posted: Fri 26 Jun 2020 6:32 am
by freddie
Probably best to do your subtraction and comparisons using epoch time, as subtraction using date objects and their fields is a bit of a minefield.

Re: Lightning notification

Posted: Fri 26 Jun 2020 8:29 am
by mcrossley
Plus 1 for using Unix timestamp.
But if you were to use dates, I'd create the two date object and use date_diff() (or the equivalent arrow function as sfw has above).

Re: Lightning notification

Posted: Fri 26 Jun 2020 1:08 pm
by beteljuice
It won't have helped having 3 y instead of 4

Code: Select all

$lightningtimenum = "<#LightningTime format="yyyMMddHHmmss">";
Edit: see viewtopic.php?p=145191#p145191 for 'final' code
You could also try ...

Code: Select all

$lightningMinAgo = (time() - mktime(<#LightningTime format="H,m,s,MM,d,yyyy">)) / 60;

<?php if ($lightningMinAgo <= 30) { echo '<span class="ligh .....

Re: Lightning notification

Posted: Fri 26 Jun 2020 1:52 pm
by Mapantz
Thank you for the replies! :D

I went with the beteljuice's as it was the most simplified for my small hippocampus, but it works!

I suppose the next question is; Can the text flash without refreshing the page, when a strike is detected?

Re: Lightning notification

Posted: Fri 26 Jun 2020 2:40 pm
by beteljuice
I suppose the next question is; Can the text flash without refreshing the page, when a strike is detected?
I suppose the answer is yes
... but not with the smb_ajax.js you are using :cry:

You would have to create a new looping 'ajax' code that could interrogate a different custom upload file (such as cuwebtags or a much smaller one with only the things you want in), and then do the logic in javascript or instruct the webtags.php to create the var you want. ... easier than it sounds :?

Re: Lightning notification

Posted: Mon 29 Jun 2020 11:44 pm
by Mapantz
I've ran in to a bit of a problem..

I updated the firmware of the GW1000 which resets lightning data. Cumulus MX defaults to show hyphens when there's no data.

$lightningMinAgo = (time() - mktime(<#LightningTime format="H,m,s,MM,d,yyyy">)) / 60;

becomes

$lightningMinAgo = (time() - mktime(---)) / 60;

It obviously stops my whole page from loading.

Is there a workaround to counter it? :oops:

Re: Lightning notification

Posted: Tue 30 Jun 2020 12:47 am
by beteljuice
Edit: see viewtopic.php?p=145191#p145191 for 'final' code

Code: Select all

$lightningMinAgo = "<#LightningTime format="H,m,s,MM,d,yyyy">";
if($lightningMinAgo != "---") $lightningMinAgo = (time() - mktime($lightningMinAgo)) / 60;


<?php if ($lightningMinAgo != "---" && $lightningMinAgo <= 30) { echo '<span class="ligh .....

Re: Lightning notification

Posted: Tue 30 Jun 2020 11:38 am
by Mapantz
Thank you beteljuice!

You helped me fix a couple of other small issues with that too. :)

Re: Lightning notification

Posted: Mon 27 Jul 2020 11:44 pm
by Mapantz
Slight issue..

It's just started flashing even though lightning was detected 16 hours ago. :shock:

Code: Select all

$lightningMinAgo = "8,5,5,07,27,2020";

Re: Lightning notification

Posted: Thu 30 Jul 2020 11:03 pm
by Mapantz
It's flashing after midnight, every single night now. I'm not sure what time it stops, somewhere between 3 and 6am, I think?!

Re: Lightning notification

Posted: Thu 30 Jul 2020 11:32 pm
by ConligWX
Mapantz wrote: Thu 30 Jul 2020 11:03 pm It's flashing after midnight, every single night now. I'm not sure what time it stops, somewhere between 3 and 6am, I think?!
Is there any caching of the values perhaps? are you running opcache at all?

Re: Lightning notification

Posted: Thu 30 Jul 2020 11:47 pm
by Mapantz
Definitely not a caching issue. The last code that beteljuice produced wasn't able to be tested until a close lightning strike 3 days ago, and so that's when it started occurring. It should only flash if the time of the last recorded strike is within the last 30 minutes. It now starts flashing each day, from midnight. I have a feeling it stops at 3am, but I am usually asleep by then. :lol:

Code: Select all

<?php if ($lightningMinAgo != "---" && $lightningMinAgo <= 30) { ..

Re: Lightning notification

Posted: Fri 31 Jul 2020 3:07 am
by beteljuice
Sounds like something strange with Cumulus on date change ....
Edit: see viewtopic.php?p=145191#p145191 for 'final' codeTime for some debug :o

Code: Select all

$lightningMinAgo = "<#LightningTime format="H,m,s,MM,d,yyyy">";
echo "\n<!-- Cu H,m,s,MM,d,yyyy = ".$lightningMinAgo." -->\n; // debug
if($lightningMinAgo != "---") $lightningMinAgo = (time() - mktime($lightningMinAgo)) / 60;
echo "\n<!-- Now: ".date(DATE_RFC822)." -->\n<!-- actual mins ago = ".$lightningMinAgo." -->\n; // debug


<?php if ($lightningMinAgo != "---" && $lightningMinAgo <= 30) { echo '<span class="ligh .....
When it goes wrong - just view page source to see what's happening.