I have a radio program and I-d like to show a block to listen to the radio online, but just while my program is on air. How can I use PHP to do so?
Thanks very much for your help.

Comments

BassistJimmyJam’s picture

Using PHP, you can get the current time and return true if it is a valid time to display the block. For example, let's say that your program is on the air from 6:00 am - 1:00 pm, you could use the following in your evalutaion:

<?php
// get the current date using a 24 digit hour without leading zeros, as an int
$current_time = (Integer) date('Gi');
if ($current_time >= '600' && $current_time <= 1300) {
    // program is on the air, display the block
    return true;
}
else {
    // program is off the air
    return false;
}
?>

In this example, if it is 12:13 pm, $current_time will be set to 1213, and the block will be displayed. If the current time is 4:15 am, $current_time will be 415 and the block will not be displayed.

nikemen’s picture

Excelent... but, how can I do to show the block during specified time but just on tuesdays?
Thanks very much!

dave reid’s picture

That would be something like:

$time = (int) date('Gi');
return (date('D') == 'Tue' && $time >= 600 && $time <= 1300);

Of course adjust the time 'hour' values to when your show is on the air.

BassistJimmyJam’s picture

You could also use the following if you prefer a numerical representation:

<?php
$time = (int) date('Gi');
return (date('w') == 2 && $time >= 600 && $time <= 1300);
?>

Checkout out date() in the php manual for for more information on the different formats you can use (http://us2.php.net/manual/en/function.date.php).

BassistJimmyJam’s picture

Status: Active » Closed (fixed)

No activity in over two weeks, closing.

tjjacobsen’s picture

In the slim chance that someone may still check this thread, I have a follow-up question:

What you show is great, but I have a slightly different need:

I need to also show the block only during certain time periods, however, mine are based on a date field within a Drupal table (specifically, I want a 'reminder' block to show when a user's Ubercart Role expiration date is approaching)

The field would be uc_roles_expirations.expiration, which is turn linked to users.uid by uc_roles_expirations.uid

I suppose I would just need to know how to pull that expiration field value into the calculation.

Any thoughts out there?

thanks!

dotidentity’s picture

-edit-
It is working for me know.

wwwoliondorcom’s picture

Hi,

Do you know any module to do the same job ?

Thanks a lot.

pinkcoffee’s picture

Hello people. Some help needed on this issue. I'm using this kind of php snippets on a D7 website, to make a block on the homepage to change at certain times every weekdays and to load a different one on Sunday.

I used codes like these:

<?php
$time = (int) date('Gi');
return (date('D') == ('Mon' || 'Tue' || 'Wed' || 'Thu' || 'Fri' || 'Sat') && $time >= 0001 && $time <= 0829);
?>  

for weekdays

and

<?php
$time = (int) date('Gi');
return (date('D') == 'Sun' && $time >= 0001 && $time <= 2459);
?>

for Sunday.

Now, everything works fine during weekdays, but on Sunday I keep seeing both the Sunday block and the ones of weekdays... what could be the problem?

Thanks for support!!

nevets’s picture

Instead of

return (date('D') == ('Mon' || 'Tue' || 'Wed' || 'Thu' || 'Fri' || 'Sat') && $time >= 0001 && $time <= 0829);

try

return (date('w') > 0 && $time >= 0001 && $time <= 0829);

Also, in the future, please post new issues in a more appropriate forum (probably post installation).

leobaker’s picture

I am using this code.

$time = (int) date('Gi');
return (date('D') == ('Mon' || 'Tue' || 'Wed' || 'Thu' || 'Fri' || 'Sat' || 'Sun') && $time >= 0001 && $time <= 0359);

As soon as my code hits Midnight 0000 it does not show. What am I doing wrong?

manoloka’s picture

Issue summary: View changes

my mistake