Click to See Complete Forum and Search --> : Store Current Date in .txt file + incremented number
F.Danials
08-11-2008, 08:52 PM
Hiya,
How do I store todays date, in a text file along with a number (starting at 1)?
If today's date is stored already inside the text file, can the number be incremented by 1?
If today's date is not already stored inside the text file, can the date be added on a new line of the text file, and the number incremented by one?
Example Text file:
Monday, 12nd, March 2008 (3)
Thursday, 03, July 2008 (39)
Saturday, 27, August 2008 (13)
scragar
08-11-2008, 10:02 PM
$file = './SOMEFILE.txt';
$date = date('l, dS, F Y');
if( file_exists($file) ){// file doesn't exist.
file_put_contents($file, "$date (1)");
}else{
$str = file_get_contents($file);
if(strpos($str, $date) === false){// not already entered
file_put_contents($file, "$str\n$date (1)");
}else{
$sp = explode($date.' (', $str);// get integer on it's own
$sp[1] = str_replace(')', '', sp[1]);// remove redundant closing bracket
$sp[1] = intval($sp[1])+1;
file_put_contents($file, "{$sp[0]}$date ({$sp[1]})");
};
};
F.Danials
08-12-2008, 01:13 PM
Why do I receive the following error message?:
Parse error: syntax error, unexpected '[' in F:\Xampp\htdocs\Tracking.php on line 12
scragar
08-12-2008, 06:00 PM
fix the line:
$sp[1] = str_replace(')', '', sp[1]);// remove redundant closing bracket
by adding a $ infront of the second reference to sp:
$sp[1] = str_replace(')', '', $sp[1]);// remove redundant closing bracket
F.Danials
08-13-2008, 09:45 AM
Each time the script is ran, is it possible to increment the number next to today's date?
scragar
08-13-2008, 06:30 PM
that should work, if it's not make a couple of edits to the script, see what info we can pull out of it, and we'll go from there:
$debug = 1;// turn off later by setting to 0
$file = './SOMEFILE.txt';
$date = date('l, dS, F Y');
echo $debug?"DEBUG START:<br>$file<br>$date<br>":'';
if( file_exists($file) ){// file doesn't exist.
file_put_contents($file, "$date (1)");
}else{
$str = file_get_contents($file);
if(strpos($str, $date) === false){// not already entered
file_put_contents($file, "$str\n$date (1)");
}else{
$sp = explode($date.' (', $str);// get integer on it's own
echo $debug?"{$sp[0]}<br>{$sp[1]}<br>":'';
$sp[1] = str_replace(')', '', $sp[1]);// remove redundant closing bracket
echo $debug?"{$sp[1]}<br>":'';
$sp[1] = intval($sp[1])+1;
if($debug){
var_dump($sp[1]);
};
file_put_contents($file, "{$sp[0]}$date ({$sp[1]})");
if($debug){
echo '<br>' . nl2br(file_get_contents($file));
};
};
};
F.Danials
08-14-2008, 09:35 AM
The number in the brackets doesn't increment, if the script is executed more than once.
scragar
08-14-2008, 09:41 AM
and the debug info shows...?