filemtime problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grudgeuk
Forum Newbie
Posts: 8
Joined: Fri Apr 07, 2006 6:27 am

filemtime problem

Post by grudgeuk »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:3. Do not make multiple, identical posts. This is viewed as spam and will be deleted.
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

}
?>
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Why are you applying the date function? Compare the output of filemtime() to time() .
grudgeuk
Forum Newbie
Posts: 8
Joined: Fri Apr 07, 2006 6:27 am

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
grudgeuk
Forum Newbie
Posts: 8
Joined: Fri Apr 07, 2006 6:27 am

Post 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 

} 
?>
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post 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

}
grudgeuk
Forum Newbie
Posts: 8
Joined: Fri Apr 07, 2006 6:27 am

Post 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
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

Post 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.
Post Reply