How would I echo a message for an hour, or a minute?
I can't just use date/time, right? That would only display it for a second...?
[RESOLVED] Displaying a message for a timeframe
For an hour:
<?php
if(date('H') == '22')
{
// IF it is 10 PM (any day) display this message
echo 'It must be 10 pm if you\'re seeing this message';
}
?>
For a minute:
<?php
if(date('i') == '30')
{
// It's half-past any hour....
echo 'It must be Oh-Dark thirty if you\'re seeing this message';
}
?>
You can do it with date functions....
If there's no alarm, how would I make it display a message? And if there's two alarms active, how would I add a linebreak between them?
alarm? What? What are you talking about? You want to show a message during X hour right?
Use the first code, and replace "22" with whatever hour (in 24 hour format) you want. an even more general code:
<?php
$hour = '14'; // Whatever 24-hour hour you want (14 = 2pm)
if(date('H')==$hour){
// Display your message, or whatever
}
?>
If you want it between 8:15am and 9:14am: compare the hours and minutes together...
I don't know what you mean about alarms... please explain that
Oops... I'm using this as an Alarms system. If it matches the day/hour, it'll display a message.
If there's no alert (I've got about 5), how can I get it to display a message? If there's over 1 alert active, how would I get it to echo a linebreak between the two?
[man]echo[/man] a "<br>" for a line break...
And for having however many alarms, this might work:
<?php
$alarms = array(
0=>array('day'=>'Mon', 'hour'=>'8', 'msg'=>'Wake Up Sleepy Head!!'),
1=>array('day'=>'Tue', 'hour'=>'11', 'msg'=>'Time for class!!'),
2=>array('day'=>'Wed', 'hour'=>'14', 'msg'=>'Time for work!!'),
3=>array('day'=>'Thu', 'hour'=>'17', 'msg'=>'Time for dinner!!'),
4=>array('day'=>'Fri', 'hour'=>'22', 'msg'=>'Time for bed!! Go to sleep!!')
);
// add each alarm in same format as others above....
// Day: three-letter representation: Sun, Mon, Tue, Wed, Thu, Fri, Sat
// Hour: numerical rep of 24 hour format (no leading zeros): 0 - 24
// msg: The alarm message you want displayed
// A variable to hold our alarms output:
$message = ''; // Set this as an empty string by default!
// Now, to display them, do a simple loop and see if the current hour (date('G'))
// is equal to the alarm hour ($alarms[$i]['hour']). If so, add it to the $message
// otherwise, don't add anything, and continue.
foreach($alarms as $alarm)
{
// Is today the same for the day of the current alarm?
if(date('D') == $alarm['day'])
{
// Okay, so it's the same day, how about hour?
if(date('G') == $alarm['hour'])
{
// Same hour and day, must mean the alarm is active!!
$message .= $alarm['msg'];
$message .= '<br>'; // Add a line-break....
}
}
}
// So now we've got a populated $message variable. But what about if no
// alarms are active? Then $message would be empty.....
if(empty($message))
{
// If the $message var is empty (it's default value),
// write a default "No Alarms" message
$message = 'No Alarms active for this time...';
}
// Now all we need to do is display the message:
echo $message;
?>
That should get you close to what you want/need.
Thanks, works very nicely.
Fantastic... glad I coudl help!!
One last question:
<?php
putenv("TZ=America/Vancouver");
$alarms = array(
0=>array('date'=>'', 'day'=>'3', 'hour'=>'', 'min'=>'', 'msg'=>'<img src="http://freepgs.com/neoaddictboards/buttons.png" alt="" /><br /><font face="Verdana" size="2" color="red">The <a href="http://www.neopets.com/winter/snowager.phtml" target="_blank">Snowager</a> is asleep!</font>'),
1=>array('date'=>'Wed', 'day'=>'', 'hour'=>'7', 'min'=>'', 'msg'=>'<img src="http://freepgs.com/neoaddictboards/buttons.png" alt="" /><br /><font face="Verdana" size="2" color="grey">Drop by the <a href="http://www.neopets.com/donations.phtml" target="_blank">Money Tree</a> for Balthazars Faerie Giveaway!</font>'),
2=>array('date'=>'Sun', 'day'=>'', 'hour'=>'5', 'min'=>'50', 'msg'=>'<img src="http://freepgs.com/neoaddictboards/buttons.png" alt="" /><br /><font face="Verdana" size="2" color="purple">Drop by the <a href="http://www.neopets.com/donations.phtml" target="_blank">Money Tree</a> for Balthazars Faerie Giveaway!</font>'),
3=>array('date'=>'', 'day'=>'', 'hour'=>'7', 'min'=>'', 'msg'=>'<img src="http://freepgs.com/neoaddictboards/buttons.png" alt="" /><br /><font face="Verdana" size="2" color="teal">Drop by the <a href="http://www.neopets.com/donations.phtml" target="_blank">Money Tree</a> for the Health Food Giveaway!</font>'),
4=>array('date'=>'', 'day'=>'', 'hour'=>'0', 'min'=>'', 'msg'=>'<img src="http://freepgs.com/neoaddictboards/buttons.png" alt="" /><br /><font face="Verdana" size="2" color="darkkhaki">Hurry! Go to <a href="http://www.neopets.com/worlds/deadlydice.phtml" target="_blank">Deadly Dice</a> before you miss your chance!</font>'),
5=>array('date'=>'Mon', 'day'=>'', 'hour'=>'10', 'min'=>'45', 'msg'=>'<img src="http://freepgs.com/neoaddictboards/buttons.png" alt="" /><br /><font face="Verdana" size="2" color="darkkhaki">Drop by the <a href="http://www.neopets.com/donations.phtml" target="_blank">Money Tree</a> for Muntando Fruit!</font>'),
6=>array('date'=>'Mon', 'day'=>'', 'hour'=>'22', 'min'=>'45', 'msg'=>'<img src="http://freepgs.com/neoaddictboards/buttons.png" alt="" /><br /><font face="Verdana" size="2" color="darkkhaki">Drop by the <a href="http://www.neopets.com/donations.phtml" target="_blank">Money Tree</a> for Muntando Fruit!</font>'),
7=>array('date'=>'', 'day'=>'', 'hour'=>'6', 'min'=>'', 'msg'=>'<img src="http://freepgs.com/neoaddictboards/buttons.png" alt="" /><br /><font face="Verdana" size="2" color="red">The <a href="http://www.neopets.com/winter/snowager.phtml" target="_blank">Snowager</a> is asleep!</font>'),
8=>array('date'=>'', 'day'=>'', 'hour'=>'14', 'min'=>'', 'msg'=>'<img src="http://freepgs.com/neoaddictboards/buttons.png" alt="" /><br /><font face="Verdana" size="2" color="red">The <a href="http://www.neopets.com/winter/snowager.phtml" target="_blank">Snowager</a> is asleep!</font>'),
9=>array('date'=>'', 'day'=>'', 'hour'=>'22', 'min'=>'', 'msg'=>'<img src="http://freepgs.com/neoaddictboards/buttons.png" alt="" /><br /><font face="Verdana" size="2" color="red">The <a href="http://www.neopets.com/winter/snowager.phtml" target="_blank">Snowager</a> is asleep!</font>')
);
// add each alarm in same format as others above....
// Day: three-letter representation: Sun, Mon, Tue, Wed, Thu, Fri, Sat
// Hour: numerical rep of 24 hour format (no leading zeros): 0 - 24
// msg: The alarm message you want displayed
// A variable to hold our alarms output:
$message = ''; // Set this as an empty string by default!
// Now, to display them, do a simple loop and see if the current hour (date('G'))
// is equal to the alarm hour ($alarms[$i]['hour']). If so, add it to the $message
// otherwise, don't add anything, and continue.
foreach($alarms as $alarm)
{
// Is today the same for the day of the current alarm?
if(date('D') == $alarm['date'])
{
if(date('j') == $alarm['day'])
{
// Okay, so it's the same day, how about hour?
if(date('H') == $alarm['hour'])
{
if(date('m') == $alarm['min'])
{
// Same hour and day, must mean the alarm is active!!
$message .= $alarm['msg'];
$message .= '<br /><br />'; // Add a line-break....
}
}
}
}
}
// So now we've got a populated $message variable. But what about if no
// alarms are active? Then $message would be empty.....
if(empty($message))
{
// If the $message var is empty (it's default value),
// write a default "No Alarms" message
$message = '<font face="Verdana" size="2" color="maroon">No Neopets Alarms right now!</font>';
}
// Now all we need to do is display the message:
echo $message;
?>
I added a few extra array keys, but for some of them, I don't have any values for them because I don't know the exact week of the day that the third is going to occur on, etc
So then you need to modify your foreach() loop and account for the empty spots and non-empty spots. Something like (and it's no guarantee this will work):
foreach($alarms as $alarm)
{
// Is today the same for the day of the current alarm?
if(!empty($alarm['day']))
{
if(date('D') == $alarm['day'])
{
// How about those times...:
if(empty($alarm['hour']) && empty($alarm['min']))
{
$message .= $alarm['msg'].'<br>';
}
else
{
if(!empty($alarm['hour']) && empty($alarm['min']))
{
if(date('G') == $alarm['hour'])
{
// Same hour and day, must mean the alarm is active!!
$message .= $alarm['msg'];
$message .= '<br>'; // Add a line-break....
}
}
elseif(!empty($alarm['min']) && empty($alarm['hour']))
{
if(date('i') == $alarm['min'])
{
$message .= $alarm['msg'].'<br>';
}
}
else
{
if((date('G') == $alarm['hour']) && (date('i')==$alarm['min']))
{
$message .= $alarm['msg'].'<br>';
}
}
}
}
}
else
{
// Will execute every month on X day (3rd for example)
if(empty($alarm['hour']) && empty($alarm['min']))
{
$message .= $alarm['msg'].'<br>';
}
else
{
if(empty($alarm['min']) && !empty($alarm['hour']))
{
if(date('G') == $alarm['hour'])
{
$message .= $alarm['msg'].'<br>';
}
}
elseif(empty($alarm['hour']) && !empty($alarm['min']))
{
if(date('i')==$alarm['min'])
{
$message .= $alarm['msg'].'<br>';
}
}
else
{
if( (date('G')==$alarm['hour']) && (date('i')==$alarm['min']) )
{
$message .= $alarm['msg'].'<br>';
}
}
}
}
}
I think it's missing the date of the week.
So add it..... I've given you plenty to go off of....
foreach($alarms as $alarm)
{
// Is today the same for the day of the current alarm?
if(!empty($alarm['day']))
{
if(date('j') == $alarm['day'])
{
// How about those times...:
if(empty($alarm['hour']) && empty($alarm['min']))
{
$message .= $alarm['msg'].'<br /><br />';
}
else
{
if(!empty($alarm['hour']) && empty($alarm['min']))
{
if(date('G') == $alarm['hour'])
{
// Same hour and day, must mean the alarm is active!!
$message .= $alarm['msg'];
$message .= '<br /><br />'; // Add a line-break....
}
}
elseif(!empty($alarm['min']) && empty($alarm['hour']))
{
if(date('m') == $alarm['min'])
{
$message .= $alarm['msg'].'<br /><br />';
}
}
elseif(empty($alarm['day']))
{
if(date('D') == $alarm['date'])
{
$message .= $alarm['msg'].'<br /><br />';
}
}
elseif(empty($alarm['day']) && empty($alarm['min']))
{
if(date('D') == $alarm['date'])
{
$message .= $alarm['msg'].'<br /><br />';
}
}
else
{
if((date('G') == $alarm['hour']) && (date('m')==$alarm['min']))
{
$message .= $alarm['msg'].'<br /><br />';
}
}
}
}
}
else
{
// Will execute every month on X day (3rd for example)
if(empty($alarm['hour']) && empty($alarm['min']))
{
$message .= $alarm['msg'].'<br /><br />';
}
else
{
if(empty($alarm['min']) && !empty($alarm['hour']))
{
if(date('G') == $alarm['hour'])
{
$message .= $alarm['msg'].'<br /><br />';
}
}
elseif(empty($alarm['hour']) && !empty($alarm['min']))
{
if(date('m')==$alarm['min'])
{
$message .= $alarm['msg'].'<br /><br />';
}
}
else
{
if( (date('G')==$alarm['hour']) && (date('m')==$alarm['min']) )
{
$message .= $alarm['msg'].'<br /><br />';
}
}
}
}
}
That should cover all the combinations, right?
just need to update the elseif() statements to use $alarm['date'] instead of $alarm['day']....
I think it's working - I'll post a new topic if it doesn't.