comparing two unix time files to delete old files
I am using the below bit of code to get the timestamps
of files on my cloud server and then delete any that are over a certain age.
For now the code is set to delete anything older than 10 minutes for testing.
However it deletes everything, even if I upload some files then run the script 10 seconds later.
Is there some small problem I am not seeing?
$auth->authenticate();
//connect
$conn = new CF_Connection($auth);
//create a handle for the CLOUD FILES container we want to delete from
$daily_dose = $conn->get_container('daily_dose');
//delete the obeject
$daily_packages = $daily_dose->list_objects();
//print_r($daily_packages);
//$public_container = $conn->get_container("public");
//$file_age = 86400; // 1 day in seconds
$file_age = 600; // 10 minutes
echo 'current time: ';
echo $current_time = time();
echo '<br>';
echo 'time offset: ';
echo $previous_day = $current_time - $file_age;
echo '<br>';
foreach($daily_packages as $package)
{
echo $item = $daily_dose->get_object($package);
$modified =开发者_Python百科 $item->last_modified;
echo ' ' . $modified;
echo ' -> ' . strtotime($modified);
echo '<br>';
//If the modified time of the file is less the current time - 1 day in seconds (older than 1 day) delete it
if ( $modified < $previous_day )
{
//delete the file
$daily_dose->delete_object($item);
}
}
If you are calling strtotime() on $modified, I guess it is not a UNIX timestamp already. time() does return a UNIX timestamp.
Shouldn't the check, then, be as follows:
if(strtotime($modified) < $previous_day)
That way you are comparing UNIX timestamps and, given that $item->last_modified returns the correct value, your code should work.
If not, make sure there are no time or timezone differences between what time() returns, and the software setting the last_modified value on your $items (easy to check with some debug output)
精彩评论