1

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.

LF00
  • 27,015
  • 29
  • 156
  • 295
omega1
  • 1,031
  • 4
  • 21
  • 41

1 Answers1

2

You can just use below code. When a new month begin, it'll create and write a new month file, otherwise it will append to current month file.

Save it monthly,

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

or save it weekly,

file_put_contents('/var/www/html/reports/' . date("Y_W") . "_report.csv", $content, FILE_APPEND);
LF00
  • 27,015
  • 29
  • 156
  • 295
  • Thanks, but I was demonstrating my code which saves a monthly file, I need to save it weekly, but thanks for the code enhancement! – omega1 Nov 13 '19 at 10:11
  • just chage `date("Y_m")` to `date("Y_W")` – LF00 Nov 13 '19 at 10:12
  • Thanks for your reply. I was actually just researching that, do you know when the `date('W')` changes to the next value? If it is Monday 00:00:00 then this will work perfectly! – omega1 Nov 13 '19 at 10:14
  • Check the [Demo](http://sandbox.onlinephpfunctions.com/code/226f5db4219ec8bd3c0ee5fc6b23b9b3b8fdcd20) – LF00 Nov 13 '19 at 10:22