Page 2 of 3

Re: RecentOutsideTemp - one year ago?

Posted: Thu 28 Jan 2016 3:42 am
by PaulMy
Hi Frank,
Enough for tonight... I can get the script to echo
Temperatur: -7.3 °C
Temperatur: -7.3 °C
Temperatur: -7.4 °C
Temperatur: -7.4 °C
Temperatur: -7.3 °C
Temperatur: -7.3 °C
and on and on... till the last entry for January 2015 (prints the temperature for each of the 10 min interval in Jan15log.txt = 4,464 records). Is that the purpose of the script or only to get the high and low of the day?

I have the date it as:
date_default_timezone_set('America/Toronto');
$now = time();
$now -= $now % 60; //remove the seconds-display
//$past = strtotime(date('d-m-Y H:i', $now) . ' - 1 year'); //subtract 1 year
$past = strtotime(date('d/m/y H:i', $now) . ' - 1 year'); //subtract 1 year

Regards,
Paul

Re: RecentOutsideTemp - one year ago?

Posted: Thu 28 Jan 2016 9:45 am
by wetterfrosch1971
try this script:

Code: Select all

<?php
  
  
  date_default_timezone_set('America/Toronto'); //local date 
  $now  = time(); //local time
  $now -= $now % 60; //remove the seconds
  $past = strtotime(date('d-m-Y H:i', $now) . ' - 1 year'); //subtract one year from aktually date/time

  
  $lines = file(strtolower (date('F')).'.txt'); //read the monthly.txt Datei
                                                             //each monthly txt-datei must be on Webspace on same place as this script
                                                             //rename the monthly-txt-datei into january.txt, february.txt, etc (small letters) 

  foreach ($lines as $row){
    $row = explode(';', $row);

    $teileDat  = explode('.',$row[0]);
    $teileZeit = explode(':',$row[1]);

    $datstamp = mktime($teileZeit[0],$teileZeit[1],'00',$teileDat[1],$teileDat[0],$teileDat[2]);


    if($datstamp === $past)  //compare the actually date/time (- 1 year) with the date/time from one year ago
     {
   
	 echo 'Temperature last year: '.$row[2].' &deg;C<br>'; //date/time this year (- 1year) the same as last year, post the temperature from last year
	  
    }
    else {
      //ERROR 
     
	 
    }
}


?>
when it don´t work, the Problem is than the time-code or the date-code, how is the sign from Toronto-date?

Code: Select all

date_default_timezone_set('America/Toronto');
it must be so: day.month.year (year only 16 not 2016)
for example: 28.01.16

or in the time-code

Code: Select all

$now  = time();
the time must be so: hour:minute:seconds
for example: 10:45:32

how is the date and the time displaed in january.txt-Datei?
ist must bei also day.month.year and the time must be hour:minute

my script only works with this exactly date und time-code, when you have in Toronto a other date and time-code, the script must be change.

on my Webspace in germany, the script run perfect.

Re: RecentOutsideTemp - one year ago?

Posted: Thu 28 Jan 2016 4:53 pm
by PaulMy
Thanks Frank for your patience. I will give it another go this evening when I am home.

Regards,
Paul

Re: RecentOutsideTemp - one year ago?

Posted: Fri 29 Jan 2016 4:16 am
by PaulMy
Well another evening at trying to work with PHP, and maybe tomorrow will be better...

I tried the script as downloaded but that gives errors. I know that the date and time need to be changed to suit by monthlog files format so changing one code at a time. Not much progress and the only thing showing is http://www.komokaweather.com/weather/mo ... r-ago2.php which shows some output from additional echo code I have placed in the error section so I could see my progress.

my Jan15log.txt format is 14/01/15,11:20,-11.8,89,-13.3,0.0,0.0,0,0.0,0.0,1030.62,...

so had to make changes for the separators in date, and data.

It is now to point I can't give up until I have something sensible :lol:

Paul

Re: RecentOutsideTemp - one year ago?

Posted: Fri 29 Jan 2016 6:12 am
by wetterfrosch1971
if your txt-datei in the date has "/" instead of "." you must change in the script this part

Code: Select all

$teileDat  = explode('.',$row[0]);
into

Code: Select all

$teileDat  = explode('/',$row[0]);

Re: RecentOutsideTemp - one year ago?

Posted: Fri 29 Jan 2016 9:05 am
by laulau
and the text separator

Code: Select all

$row = explode(';', $row);
to

Code: Select all

$row = explode(',', $row);

Re: RecentOutsideTemp - one year ago?

Posted: Fri 29 Jan 2016 3:21 pm
by wetterfrosch1971
laulau wrote:and the text separator

Code: Select all

$row = explode(';', $row);
to

Code: Select all

$row = explode(',', $row);
thank you Laurent, it is right!

@Paul

Change this two code, and try the script again

Code: Select all

$row = explode(',', $row);

Code: Select all

$teileDat  = explode('/',$row[0]);

Re: RecentOutsideTemp - one year ago?

Posted: Fri 29 Jan 2016 7:41 pm
by PaulMy
Progress report after several more hours - not good yet!
I had originally changed the ";" to "," and the "-" to "/" which produced no results. I have added some new echo in the "else error" section so I could see what the variables are giving and that seems ok to me http://www.komokaweather.com/weather/mo ... r-ago2.php

The $row[0], [1], [2] , [3], [4], etc. gives the final data for each of the fields in january.txt which is for 2015
datestamp is code for January 31, 2015 I think
time is code for today

Just can't seem to get the data for current date/time. It has been a great experience getting and testing variables in PHP but can't seem to get a better understanding and not making much progress. One last call before I give up, what do you think is wrong with the code?

Code: Select all

<?php
	// GET TODAY'S DATA
	// From monthly Log File
  
  date_default_timezone_set('America/Toronto'); //local date 
  $now  = time(); //local time
  $now -= $now % 60; //remove the seconds
  $past = strtotime(date('d/m/Y H:i', $now) . ' - 1 year'); //subtract one year from aktually date/time

  
  $lines = file(strtolower (date('F')).'.txt'); //read the monthly.txt Datei
    //each monthly txt-datei must be on Webspace on same place as this script
    //rename the monthly-txt-datei into january.txt, february.txt, etc (small letters) 

  foreach ($lines as $row){
    $row = explode(',', $row);

    $teileDat  = explode('/',$row[0]);
    $teileZeit = explode(':',$row[1]);

    $datstamp = mktime($teileZeit[0],$teileZeit[1],'00',$teileDat[1],$teileDat[0],$teileDat[2]);

    if($datstamp === $past)  //compare the actually date/time (- 1 year) with the date/time from one year ago
     {
   
    echo 'Temperature last year: '.$row[2].' &deg;C<br>'; //date/time this year (- 1year) the same as last year, post the temperature from last year
     
    }
    else {
      //ERROR 
     //echo 'error'.'<br />';
		
    }
}

     echo 'Row 0: '.$row[0].'<br>';
		 echo 'Row 1: '.$row[1].'<br>';
		 echo 'Row 2: '.$row[2].'&deg;C<br>';
		 echo 'Row 3: '.$row[3].'<br>';
		 echo 'Row 4: '.$row[4].'<br>';
		 echo 'datestamp one year ago: ' .$datstamp .'<br />';
		 echo 'now; ' .date('d/m/y'). '<br />';
		 Echo	'time: '.time('h:m'). '<br />';
		 
?>
Thank you for the help in PHP-101

Enjoying, I think
Paul

Re: RecentOutsideTemp - one year ago?

Posted: Fri 29 Jan 2016 8:49 pm
by wetterfrosch1971
your "datestamp one year ago" is now correct.

now change your code

Code: Select all

$past = strtotime(date('d/m/Y H:i', $now) . ' - 1 year');
into

Code: Select all

$past = strtotime(date('d-m-Y H:i', $now) . ' - 1 year');
if you echo "$past" it must be in the same structure as "$datstamp" -> 1422766200 -> so without strokes or Points, only numbers!
The result from $datstamp and $past must be the same!

and try it again

it must be work, I´ve try on my Webspace with Toronto-time and it works, but you must Change the code $past so how I explain in the beginning of this post.

Re: RecentOutsideTemp - one year ago?

Posted: Fri 29 Jan 2016 10:53 pm
by PaulMy
Hi Frank,
From the results of the various echos, it doesn't seem to make a difference weather the date is d-m-Y or d/m/Y. Also, weather using Europe/Berlin or America/Toronto doesn't make a difference. After some more testing it looks that the existing code has
$datstamp returning 1422766200 which is 2015-01-31-23:50 and that is the last entry in January.txt (from Jan15log.txt) and this is incorrect.
$past returning 1422568680 which is the current time minus 1 year (2016-01-29-17:03 minus 1 year = 2015-01-29-17:03) and this is correct.
http://www.komokaweather.com/weather/mo ... r-ago2.php

I think I have figured out the pieces that make up $datstamp from $datstamp=(mktime $teileZeit[0], etc. being Hour Min Month Day and Year but not familiar with mktime and how it produces the time.

How to make $datstamp = the expected day i.e.,2015-01-29-xx:xx is the struggle.

A couple of last comments, questions; I am using the script within a simple <! DOCUMENT html PUBLIC ....><html> <head> </head> <body> </body> </html> code borrowed from another page. Could that have any impact on this script or PHP? or the version of PHP on my server?

Regards,
Paul

Re: RecentOutsideTemp - one year ago?

Posted: Sat 30 Jan 2016 2:42 am
by wetterfrosch1971
put for a test the complete script in a Editor, safe it local on your Computer as a name.php and upload this file to your Webspace (without html etc).
for example my script called "datum.php"
http://www.wetterstation-badenweiler.de ... /datum.php

a other question:
wich is your data-log-Intervall on cumulus-configuration, 1mins, 5mins, 10mins, 15mins or 20 mins?

the script runs only with data-log-intervall 1 mins !
your january.txt must be logged every minutes, but you say, your last data in january.txt is 31. january at 23:50 Uhr so I think, your log-Intervall ist every 10 minutes!?

if it´s like that,
the script must be change, because for example if the time now 23:46 but your januray.txt has only 23:40 or 23:50, so find the script no results because this condition in the script is wrong "$datstamp === $past"

you can try mayby this code unstead "$datstamp === $past"

Code: Select all

if($datstamp =< $past)
but I think, it doesen´t work, because so all results which are bigger as the actually time are correct.
if you have luck, the result is the first entry wich is bigger, than it works, but if you have no luck, the results are ALL entries wich are bigger.
so the script must be stopped, if it found the first result, but how to do that, I don´t know ;-(

mayby a other sulution:
you must be round the actually time to 10 minutes, for example:
the time now is 23:46 and the script must be round this to 23:40
analogous to round the seconds in this part

Code: Select all

$now = $now % 60; //remove the seconds
but how do that, I don´t know in the moment.


I try in the evening (here in Germany it is now 4:30 in the morning) a solution for the problem if you logged your january.txt only every 10 minutes.

Re: RecentOutsideTemp - one year ago?

Posted: Sat 30 Jan 2016 4:41 am
by PaulMy
Hi Frank,
It has been an excellent exercise and learned a lot, but not yet ready to try to do PHP coding :bash: Yes my data is in 10 minute intervals now and in 2015. I tried the alternate but that gave an error. As I understand it the -

"foreach" creates a $datstamp for each entry with the =mktime(
"if" one of the $datstamp matches $past "then" the temperature from that line is echoed

since there is no match for $datstamp to $past it goes to the "else" and echoes what is called for.

Simple enough once I took each piece of the script and tried to follow the logic, but that wasn't easy as I don't know the PHP commands or functions. I also learned a bit about $datstamp (timestamp) from Google search. I have thought and tried about rounding to the nearest 10 minutes similar to the %60 but couldn't come up with how to do that correctly.

Thank you for your time and effort to guide me through the script and I will chalk it up as a good learning lesson.

All the best,
Paul

Re: RecentOutsideTemp - one year ago?

Posted: Sat 30 Jan 2016 6:26 pm
by wetterfrosch1971
for 10 Minute log Intervall, try this code

Code: Select all

<?php
  
  
  date_default_timezone_set('America/Toronto'); //local date 

  $now  = ceil(time()/600)*600; // round up to 10 Minutes 
     //if you want round down use this -> //$now = floor(time()/600)*600;

  $now = $now % 60; //remove the seconds
  $past = strtotime(date('d-m-Y H:i', $now) . ' - 1 year'); //subtract one year from aktually date/time

  
  $lines = file(strtolower (date('F')).'.txt'); //read the monthly.txt Datei
                                                             //each monthly txt-datei must be on Webspace on same place as this script
                                                             //rename the monthly-txt-datei into january.txt, february.txt, etc (small letters) 

  foreach ($lines as $row){
    $row = explode(',', $row);

    $teileDat  = explode('/',$row[0]);
    $teileZeit = explode(':',$row[1]);

    $datstamp = mktime($teileZeit[0],$teileZeit[1],'00',$teileDat[1],$teileDat[0],$teileDat[2]);


    if($datstamp === $past)  //compare the actually date/time (- 1 year) with the date/time from one year ago
     {
   
    echo 'Temperature last year: '.$row[2].' &deg;C<br>'; //date/time this year (- 1year) the same as last year, post the temperature from last year
     
    }
    else {
      //ERROR 
     
    
    }
}


?>

Re: RecentOutsideTemp - one year ago?

Posted: Sat 30 Jan 2016 11:41 pm
by PaulMy
Hi Frank, and :clap: :clap:
You've hit the nail on the head - www.komokaweather.com/weather/monthLog/year-ago2.php

I had attempted using 60 * 60 and *3600 which gave some different result but not the correct one. Your latest script does it correctly.

I know I have learned from this and which I may be able to apply to some other comparisons.

Again thanks for your time and effort on this. Do you have a webpage where the script is used?

Regards,
Paul

Re: RecentOutsideTemp - one year ago?

Posted: Sun 31 Jan 2016 8:57 am
by wetterfrosch1971
Hi,

I´m happy that it works ;-)

Remark to the script:
The script also work at the 29. february 2016, PHP is clever and use at 29. february 2016 for "one year ago" the date 01. March 2015

yes, I have e Webpage, this script run on this Webpage
it is the value in the table behind the current temperature, the value in clamp "()"

http://www.wetterstation-badenweiler.de ... /index.php

and my start-page ist this:
http://www.wetterstation-badenweiler.de/

I´m a beginner into webdesingn, therefore my Webpage is very simple and not so beautiful and perfect as your webpage.

Frank