Page 1 of 1
filemtime problem
Posted: Thu Aug 24, 2006 1:57 am
by grudgeuk
I have this piece of code that needs to run on a daily basis to refresh some data. It is called each time someone views the website and it checks if the modifed date on the file is different to todays date and if so it needs to run some code to refresh the file.
Now the issue I have is that it works sometimes and not others. On average it works 4 days out of 7.
I have tried debugging and it works fine, just wondering if anyone can point out the problem in the code.
Thanks
Code: Select all
<?php
$input_file = "weatherfeed/ruthindata.htm";
$output_file = "weatherfeed/5dayweather.txt";
$html_file = "weatherfeed/ruthindata.htm";
$weathermoddate = date("D d M Y",filemtime($output_file));
$today = date("D d M Y", time() );
if ($today > $weathermoddate ) {
//Lots of Code
}
?>
Posted: Thu Aug 24, 2006 3:15 am
by jamiel
Why are you applying the date function? Compare the output of filemtime() to time() .
Posted: Thu Aug 24, 2006 3:16 am
by grudgeuk
jamiel wrote:Why are you applying the date function? Compare the output of filemtime() to time() .
Sorry to be a pain - could you supply some code to do that? Or show me were I have gone wrong?
Posted: Thu Aug 24, 2006 3:22 am
by JayBird
grudgeuk wrote:
Sorry to be a pain - could you supply some code to do that? Or show me were I have gone wrong?
Remove the date() functions
Posted: Thu Aug 24, 2006 3:29 am
by grudgeuk
So am I right in thinking this is what I need -
Code: Select all
<?php
$input_file = "weatherfeed/ruthindata.htm";
$output_file = "weatherfeed/5dayweather.txt";
$html_file = "weatherfeed/ruthindata.htm";
$weathermoddate = filemtime($output_file);
$today = time();
if ($today > $weathermoddate ) {
//Lots of Code
}
?>
Posted: Thu Aug 24, 2006 3:40 am
by jamiel
I just re-read your question. If you want to go on date use this ...
Code: Select all
<?php
$input_file = "weatherfeed/ruthindata.htm";
$output_file = "weatherfeed/5dayweather.txt";
$html_file = "weatherfeed/ruthindata.htm";
$weathermoddate = date("Ymd", filemtime($output_file));
$today = date("Ymd", time());
if ($weathermoddate != $today) {
// Run code
}
Otherwise for last 24 hours do this ...
Code: Select all
<?php
$input_file = "weatherfeed/ruthindata.htm";
$output_file = "weatherfeed/5dayweather.txt";
$html_file = "weatherfeed/ruthindata.htm";
$weathermoddate = filemtime($output_file);
$yesterday = time() - (60 * 60 * 24);
if ($yesterday > $weathermoddate) {
// Code
}
Posted: Thu Aug 24, 2006 5:52 am
by grudgeuk
Changes implemented and working well.
The problem is the fact that I was comparing strings...
So "Thurs 24 Aug 2006" is < "Wed 23 Aug 2006" -- because T comes before W in the alphabet. Simple eh?
Therefore the reason why it worked on some days and not the others.
With the suggest change of the date function to:
$today = date('Ymd');
I got a nice date stamp of 20060824.
Thanks jamiel
Posted: Thu Aug 24, 2006 8:10 am
by paladaxar
Haha. I can see where that would be frustrating...some days it works, others it doesnt.
Could you please ammend [solved] to your thread title? That way people wont waste time reading through a problem that already has a solution.