0

I need to show a button on my site only certain days/times

For ex:

Sunday - 8am-8pm
Monday - Dont Show
Tuesday - 7pm-9pm
Wednseday - 4pm-11pm

and so on (repeats every week)

How can i achieve this? (using javascript or php)

Behzad
  • 3,502
  • 4
  • 36
  • 63
dkrasniy
  • 408
  • 5
  • 12

1 Answers1

1

This is one way in php:

$show_times = array(
    array('sunday 8am', 'sunday 8pm'),
    array('tuesday 7pm', 'tuesday 9pm'),
    array('wednesday 4pm', 'wednesday 11pm')
);

$now = time();
$show = false;

foreach ($show_times as $time) {
    $from = strtotime($time[0]);
    $to = strtotime($time[1]);

    if ($now >= $from and $now <= $to) {
        $show = true;
        break;
    }
}

if ($show) {
    echo '<button>the button</button>';
}
Rudi
  • 2,987
  • 1
  • 12
  • 18