dqbh8054
2017-08-03 07:44
浏览 38
已采纳

删除超过特定月份的文件

I create that script:

$zips = glob("*.zip");

usort($zips, function($a, $b) { return filemtime($a) - filemtime($b); });

$dateFile = date("F d Y H:i:s", filectime($zips[0]));
$dateNow = date("F d Y H:i:s");

echo "dateFile = " . $dateFile;
echo "<br />";
echo "dateNow = " . $dateNow

Output is:

dateFile = August 03 2017 10:23:47
dateNow = August 03 2017 10:43:27

I want to check if file is older that month a.k.a dateNow - dateFile ? in months?

图片转代码服务由CSDN问答提供 功能建议

我创建该脚本:

  $ zips = glob(  “* .zip”); 
 
inort($ zips,function($ a,$ b){return filemtime($ a) -  filemtime($ b);}); 
 
 $ dateFile = date(“  F d YH:i:s“,filectime($ zips [0])); 
 $ dateNow = date(”F d YH:i:s“); 
 
echo”dateFile =“。  $ dateFile; 
echo“&lt; br /&gt;”; 
echo“dateNow =”。  $ dateNow 
   
 
 

输出为:

  dateFile = 2017年8月3日10:23:47 
dateNow = August  03 2017 10:43:27 
   
 
 

我想检查那个月的文件是否更旧,也就是dateNow - dateFile? 几个月?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doudonglu3764 2017-08-03 07:53
    已采纳

    You do it like the following

    if(strtotime(dateFile) < strtotime('first day of last month', time())) {
       //file is older than 1 month
    }
    

    You can also use the function for 30 days from now. This will check if the file is older than 30 days backwards from the time you will do this condition.

    if(strtotime(dateFile) < (time()-2592000)) {
       //file is older than 1 month
    }
    

    2592000 are 30 days in seconds.

    点赞 评论
  • duanjizi9443 2017-08-03 07:51

    If you want to delete all files older than a month and you are on a linux machine, you can use a bash command to fulfil your request. On windows you need another command, but it works too.

    this command finds all files older than 30 days in /path/to/files this folder and deletes them. Make sure that your path is correct or wrong files get deleted!

    find /path/to/files* -mtime +30 -exec rm {} \;
    

    I forgot to post the correct PHP code!

    exec("find /path/to/files* -mtime +30 -exec rm {} \;");
    
    点赞 评论
  • drmqzb5063 2017-08-03 07:52

    get diff from current date and file date ; and check if older than 30 days delete it:

    $date1 = date_create(date("Y-m-d")); // current date
    $date2 = date_create($file_date); // change file date to Y-m-d format
    $diff  = date_diff($date1,$date2);
    if($diff->format("%R") == '+' && $diff->format("%a") != 0){
        if($diff->format("%a") > 30)
            //delete file
    }else
        //delete file
    
    点赞 评论

相关推荐 更多相似问题