首页 > 解决方案 > PHP - How to process/save weekly files

问题描述

I need to process a file by adding to it, but then saving as separate weekly files.

It is easy to do monthly, which I am doing as follows:

// Set variables
$month = date('m');
$year = date('Y');
// Load previous file content
$newreportfile = file_get_contents('/var/www/html/reports/' . $year . "_" . $month . "_report.csv");
// Add new content
$file_to_write = $newreportfile . $report_log;
// Write file back
file_put_contents('/var/www/html/reports/' . $year . "_" . $month . "_report.csv", $file_to_write);

This works fine for keeping it all in a monthly file, but how could I do this to have it in a weekly format, specifically breaking the files on a particular day of week at a particular time (Sunday 23:59:59)?

Any guidance on the best way to do this would be appreciated.

Thank you.

标签: phpfiledatetimefile-put-contents

解决方案


您可以使用以下代码。当新的月份开始时,它将创建并写入一个新的月份文件,否则它将附加到当前月份文件。

每月存起来,

file_put_contents('/var/www/html/reports/' . date("Y_m") . "_report.csv", $content, FILE_APPEND);

或每周保存一次,

file_put_contents('/var/www/html/reports/' . date("Y_W") . "_report.csv", $content, FILE_APPEND);

推荐阅读