IT.COM

Php - Show Today's Entry From Text File

Spaceship Spaceship
Watch
Hello...
I have a text file (let's say eachday.txt) and it includes...
Code:
Monday, June 14, 2010, Monday's Text
Tuesday, June 15, 2010, Tuesday's Text
Wednesday, June 16, 2010, Wednesday's Text
Thursday, June 17, 2010, Thursday's Text
Friday, June 18, 2010, Friday's Text
All I want to do is print (echo is fine) today's line on a webpage. I can handle the html formatting and stuff. Just matching today's date with the current line will get me down the road.
I have it, kind of, working from a much larger, more-encompassing, script, but I figure there is a quick, clean, bit of code that would do the job, just as well.
Anyone care to give a hand?
JJ
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
If you look at php.net/date, you'll see D is used for Mon thru Sun.

Code:
$day = date('D'); // Wed
$dayfile = sprintf('/path/to/%s.txt', $day); // /path/to/Wed.txt

if (file_exists($dayfile))
{
    echo file_get_contents($dayfile);
}


---------- Post added at 04:08 PM ---------- Previous post was at 04:03 PM ----------

If you don't want to use text files and maybe have short lines to output instead, you could do something like this...

Code:
$day = date('D'); // Wed

$daylines = array(
  'Mon' => 'output for Monday',
  'Wed' => 'output to display on Wednesdays',
  'Fri'  => 'output for Friday',
); // you get the idea here...

if (isset($daylines[$day]))
{
    echo $daylines[$day];
}
 
0
•••
Apologies...
I should have been more specific. It's a year's worth of different text to print, no a week's.
I want to match the date and show that text.
JJ
 
0
•••
The format for the source of information is important here. Is it one file? Many? Is it in a file? Is it going to be in the php? Is it in a database?

Is it 365 lines in 1 file? You could use day of year. date('z'). Assuming it is like this then maybe...
Code:
$lines = file('/path/to/365dayfile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$dayofyear = date('z'); // today is 166th day of the year
print $lines[$dayofyear];  // will print the 167th line?


---------- Post added at 05:41 PM ---------- Previous post was at 05:38 PM ----------

Sorry about that, I just re-read the original post. I see what you've got now. Give me a moment to code it.
 
0
•••
That's a very interesting bit of code. I'll look into it further.
I was able to hack up some code and make it work. I'm putting the finishing touches on it, but it is doing what I want, thusfar.
mobile-biblereading.txt
Code:
Monday June 14 2010 Mark 3
Tuesday June 15 2010 Mark 4
Wednesday June 16 2010 Mark 5
Thursday June 17 2010 Mark 6
Friday June 18 2010 Mark 7
mobile-default.php
Code:
<?php
$fp = fopen("mobile-biblereading.txt", "r");
while ($fileinfo = fscanf($fp, "%s %s %s %s %s %s\n")) {
	list($day, $month, $date, $year, $book, $chapter) = $fileinfo;
	if ("$day, $month $date, $year" == date("l, F d, Y")) {
		echo "$day, $month $date, $year - $book $chapter";
	}
}
fclose($fp);
?>
I'm sure it's not the best, but it gets the job done. I don't have any exceptions for the lack of Sunday/Saturday, yet, and the .txt isn't formatted the way I'd like. Now that's it working, though, it's easy to fix.
The problem that I was having, before, apparently stemmed from not putting "" in "$day, $month $date, $year". That was irritating. It took me an hour or two to realize that.
I'm not terribly well-versed in php, but I know enough that I can take a general example and run with it. This script was originally to make a bit of flat text a link, each day. It had the date matching with mktime, though. It took me all day, but I made it work.
I'd still appreciate some clean-up advice.
JJ
 
0
•••
You might try something like this then, assuming you have one file with a list of lines in that format and there is only 1 line that matches

Code:
$regex = sprintf('/%s(.*)/', date('l, F j, Y, '));

if (preg_match($regex, file_get_contents('/path/eachday.txt'),$match))
{
	print $match[1];
}
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back