Jump to content

adding a date column


trauch

Recommended Posts

I currently have a script that adds numbers from multiple files and then outputsthe sum (a single number like 28449358) to a new text file named as the current date (ie 2008-12-22). Although this is working I'd like to change the code so that the sum is appended to a file called total.txt. However, in addition to the number there needs to be a date stamp separated by a ";" (ie 2008-12-22;28449358). This would be appended to each day. Btw, Im using php4.

 

Here's my existing code

<?php


// Make an array of the filenames to sum
$fileNames = array('textfile.txt', 'textfile2.txt', 'textfile3.txt');

// Open the file and zero out the net sum
$fp = fopen('textmaster.txt', 'a');
$netSum = 0;

// Add the numbers and append to the file
foreach($fileNames as $fileName) {
   $content = str_replace(' ', '', file($fileName)); 
   $sum = array_sum($content);
   echo 'Sum of "' . $fileName . ': ' . $sum;
   $netSum += $sum;
   fwrite($fp, $sum);
}

// Clean up
fclose($fp);

// Create the day file
$fw = fopen(date('Y-n-j') ';');
fwrite($fw, $netSum);
fclose($fw)


?>

Link to comment
https://forums.phpfreaks.com/topic/138040-adding-a-date-column/
Share on other sites

To elaborate, my problem is that currently my output is a text file whose filename will be the current date and whose contents will be a single number (sum from script).

 

What I want is the output to be saved (appended) to an existing file (total.txt) and instead of simply providing the number, to also include a date stamp. So each day the output would be (2008-12-22;238794)

Over a series of days, the resulting total.txt would look like:

 

2008-12-22;238794

2008-12-23;525355

2008-12-24;636633

2008-12-25;455747

Link to comment
https://forums.phpfreaks.com/topic/138040-adding-a-date-column/#findComment-721554
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.