根据时间/时间表交换div可见性 [英] Swap div visibility based on time / schedule
问题描述
在我的页面上,我有两个div ...一个div我希望在上午10点至下午6点(服务器时间)可见.
On my page I have two divs... one div I'd like to be visible from 10am to 6pm ( server time ).... and the other div for the remaining hours.
我尝试了一堆搜索,以找到某种没有任何运气的JavaScript或jquery内容交换器.
I tried a bunch of searches to find some sort of a javascript or jquery content swapper without any luck.. thanks for any suggestions?
<div id="day">content</div>
<div id="night">content</div>
我只能使用以下php来使其工作:
I was able to get this working using only the following php:
<?php
$hour = strftime("%H");
if ($hour >= 02 && $hour < 05)
{
echo '<div id="div1">content block one </div>';
}
else
{
echo '<div id="div2">content block two</div>';
}?>
但是,如果我要显示从晚上8点至凌晨4点的div,则此解决方案似乎不起作用...是因为这跨越了一天以上?感谢您的任何建议.
However this solution doesn't seem to work if I want to show the div from 8pm until 4am... is this because it is spanning more than one day? Thanks for any suggestions.
推荐答案
编辑
您在评论中提到的案件是一个棘手的案件.所以这是我修改后的答案:
EDIT
The case you mentioned in your comment is a thorny one. So here is my revised-revised answer:
<?php
$t0 = 20; // from hour (inclusive) -- int, 0-23
$t1 = 4; // till hour (excluding) -- int, 0-23
$t = date('G'); // current hour (derived from current time) -- int, 0-23
if ($t0 == $t1) {
$in_range = NULL;
} elseif ($t0 < $t1) {
$in_range = ($t0 <= $t && $t < $t1);
} else {
$in_range = ($t1 <= $t && $t < $t0) == false;
}
/*
echo $in_range === NULL
? 'from and till dates must be different'
: ($in_range ? 'just in time' : 'wait till the time is right');
*/
if ($in_range === false) {
$s0 = mktime($t0, 0, 0); // lunch time
$s = time(); // current time
if ($s0 < $s) {
$s0 += 60 * 60 * 24; // late for lunch! now wait till tomorrow
}
$d0 = $s0 - $s;
$dh = floor($d0 / 60 / 60);
$dm = $d0 - $dh * 60 * 60; $dm = floor($dm / 60);
$ds = $d0 - $dh * 60 * 60 - $dm * 60;
echo sprintf("
Current date...: %s<br />
Target date....: %s<br />
Time to go.....: %d hours, %d minutes, %d seconds
",
date("Y-m-d h:i:s A", $s),
date("Y-m-d h:i:s A", $s0),
$dh,
$dm,
$ds
);
}
?>
这篇关于根据时间/时间表交换div可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!