One way you could do it is to create a reusable object and then manually look at each one to see which is current or not. E.g.:
<?php
class Page
{
private $data = array();
protected $timeFrom;
protected $timeTo;
protected $
public function __construct($timeFrom, $timeTo)
{
$this->data['timeFrom'] = $timeFrom;
$this->data['timeTo'] = $timeTo;
if(strpos($this->data['timeFrom'], ':') === false)
{
throw new Exception('Invalid time supplied: "'.$this->data['timeFrom'].'". Time format is "HH:MM".');
}
if(strpos($this->data['timeTo'], ':') === false)
{
throw new Exception('Invalid time supplied: "'.$this->data['timeTo'].'". Time format is "HH:MM".');
}
}
public function isCurrent()
{
// Is it after the begin time?
list($hour, $min) = explode(':', $this->data['timeFrom']);
if(date('H') >= $hour && date('i') >= $min)
{
// How about the end time?
list($hour, $min) = explode(':', $this->data['timeTo']);
if(date('H') < $hour && date('i') <= $min)
{
return true;
}
}
return false;
}
}
$morning = new Page('07:00', '15:00');
$morning->file = 'dir/filename.php';
$morning->image = 'dir/sunrise.jpg';
$morning->greeting = 'Good Morning! Guten Morgen!';
$evening = new Page('15:00', '23:00');
$evening->file = 'dir/filename.php';
$evening->image = 'dir/sunset.jpg';
$evening->greeting = 'Good Evening! Guten Abend!';
$night = new Page('23:00', '07:00');
$night->file = 'dir/filename.php';
$night->image = 'dir/moon.jpg';
$night->greeting = 'Good Night! Gute Nacht!';
$current = false;
if($morning->isCurrent())
{
$current = $morning;
}
elseif($evening->isCurrent())
{
$current = $evening;
}
elseif($night->isCurrent())
{
$current = $night;
}
$image = $current->image;
$file = $current->file;
$greeting = $current->greeting;
Alternatively, you could create a "registry" of objects and then create one function to loop through a collection and return that one object that is current. E.g.:
<?php
class PageRegistry
{
private $_pages = array();
public function __set($name, $val)
{
if(instanceof $val != Page)
{
throw new Exception('Page object expected, but not given');
}
$this->_pages[$name] = $val;
}
public function __get($name)
{
if(isset($this->_pages[$name]))
{
return $this->_pages[$name];
}
return false;
}
public function getCurrent()
{
foreach($this->_pages as $page)
{
if($page->isCurrent())
{
return $page;
}
}
}
}
class Page
{
private $data = array();
protected $timeFrom;
protected $timeTo;
protected $
public function __construct($timeFrom, $timeTo)
{
$this->data['timeFrom'] = $timeFrom;
$this->data['timeTo'] = $timeTo;
if(strpos($this->data['timeFrom'], ':') === false)
{
throw new Exception('Invalid time supplied: "'.$this->data['timeFrom'].'". Time format is "HH:MM".');
}
if(strpos($this->data['timeTo'], ':') === false)
{
throw new Exception('Invalid time supplied: "'.$this->data['timeTo'].'". Time format is "HH:MM".');
}
}
public function __set($name, $value)
{
$this->data[$name] = $value;
}
public function __get($name)
{
if(isset($this->data[$name]))
{
return $this->data[$name];
}
return false;
}
public function isCurrent()
{
// Is it after the begin time?
list($hour, $min) = explode(':', $this->data['timeFrom']);
if(date('H') >= $hour && date('i') >= $min)
{
// How about the end time?
list($hour, $min) = explode(':', $this->data['timeTo']);
if(date('H') < $hour && date('i') <= $min)
{
return true;
}
}
return false;
}
}
$registry = new PageRegistry;
$morning = new Page('07:00', '15:00');
$morning->file = 'dir/filename.php';
$morning->image = 'dir/sunrise.jpg';
$morning->greeting = 'Good Morning! Guten Morgen!';
$evening = new Page('15:00', '23:00');
$evening->file = 'dir/filename.php';
$evening->image = 'dir/sunset.jpg';
$evening->greeting = 'Good Evening! Guten Abend!';
$night = new Page('23:00', '07:00');
$night->file = 'dir/filename.php';
$night->image = 'dir/moon.jpg';
$night->greeting = 'Good Night! Gute Nacht!';
$registry->morning = $morning;
$registry->evening = $evening;
$registry->night = $night;
$current = $registry->getCurrent();
$image = $current->image;
$file = $current->file;
$greeting = $current->greeting;
I'm not saying these would work, they're more or less just pseudo-code to get you started.
The procedural way would be to just add "timeTo" and "timeFrom" keys with the time values you want and use the same logic as in the isCurrent() method of the Page object.
Hope that helps in some way....