Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date and Time question

Status
Not open for further replies.

smoky989

Programmer
Jan 9, 2003
3
ok this seems common enough but my head hurts so I'll ask you guys since you haven't failed me yet.

I want to have a PHP page only display during a certain time frame. I have four pages I want to do this for.

Page 1 - I want viewable from 11PM Friday until 1159 PM on Tuesday
Page 2 - I want viewable from 11PM Saturday until 1159 PM on Wednesday
Page 3 - I want viewable from 11PM Monday until 1159 PM on Friday
Page 4 - I want viewable from 11PM Tuesday until 1159 PM on Saturday

Any ideas for code? could I use something as simple as

Code:

if(var for current date > var for beginning date && var for current date < var for end date)
{
Content of the page
}
 
Since your begin and end times overlap, I assume that you want a script that can display any of 4 blocks of content, depending on which time of the week it is.

You could do it with if-blocks, but then your code has to be changed should you ever remove a block of content to display or add a 5th block.

Here's one way, using a configuration file...

Put your 4 content blocks in files outside your main script.

Create a configuration file which reads something like this:
Code:
[content1]
begin = 52300
end = 22359
file = test_fileshow_content1.html

[content2]
begin = 62300
end = 32359
file = test_fileshow_content2.html

[content3]
begin = 12300
end = 52359
file = test_fileshow_content3.html

[content4]
begin = 22300
end = 62359
file = test_fileshow_content4.html

Then parse your config file into an array and loop through it in a foreach, including the 4 content files:

Code:
<?php
$foo = parse_ini_file ('test_fileshow.ini', TRUE);

$bar = date ('wHi');

foreach ($foo as $content)
{
	if ($content['end'] >= $content['begin'])
	{
		if ($bar >= $content['begin'] && $bar <= $content['end'])
		{
			include($content['file']);
		}
	}
	else
	{
		if (($bar >= $content['begin'] && $bar <= 61159) ||
		    ($bar >= 00000 && $bar <= $content['end']))
		{
			include ($content['file']);
		}
	}
}
?>[code]

If you need to add a 5[sup]th[/sup] block to the mix, create a fifth content file and add that file's spec to your configuration file.
 [i]Want the best answers?  Ask the best questions:[/i] [URL unfurl="true"]http://www.catb.org/~esr/faqs/smart-questions.html[/URL]
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top