如何在PHP中为不同的时间范围显示不同的内容?
尝试编写一个页面的一部分,当我们在5a-10a M-F上播放时显示流媒体播放器,当我们不在播放时显示其他内容 这是我现在拥有的,但它不起作用。我对PHP相当陌生。谢谢如何在PHP中为不同的时间范围显示不同的内容?,php,Php,尝试编写一个页面的一部分,当我们在5a-10a M-F上播放时显示流媒体播放器,当我们不在播放时显示其他内容 这是我现在拥有的,但它不起作用。我对PHP相当陌生。谢谢 <html> <head> <title>streaming</title> </head> <body> <?php //Get the current hour $current_time = date(G); //Get the current
<html>
<head>
<title>streaming</title>
</head>
<body>
<?php
//Get the current hour
$current_time = date(G);
//Get the current day
$current_day = date(l);
//Off air
if ($current_day == "Saturday" or $current_day == "Sunday" or ($current_time <= 5 && $current_time >= 10)) {
echo "We’re live Monday – Friday mornings. Check back then.";
}
// Display player
else {
echo "<a href="linktoplayer.html"><img src=http://www.psdgraphics.com/wp-content/uploads/2009/09/play.jpg></a>";
}
?>
</body>
</html>
流动
#1。
这种说法永远是错误的:
($current_time <= 5 && $current_time >= 10)
正确:
($current_time < 5 || $current_time >= 10)
$current_time = date('G');
$current_day = date('l');
#3.
此代码echo”“代码>还将输出解析错误:
Parse error: syntax error, unexpected 'linktoplayer' (T_STRING), expecting ',' or ';' in ...
如果要输出字符串,则必须使用字符串中的\
转义“
:
echo "<a href=\"linktoplayer.html\"><img src=\"http://www.psdgraphics.com/wp-content/uploads/2009/09/play.jpg\"></a>";
p、 图像的s.src
属性必须用“
”两个东西括起来:
正如已经指出的,您的时间逻辑已关闭。它应该是$current\u time<5或$current\u time>=10)
当您给日期函数赋值时,它必须是一个字符串。date(l)
将抛出一个错误,因为它应该是date('l')
编辑:
如果你真的想对你的代码进行基准测试,你应该使用idate
,因为它返回一个整数。你的比较结果如下:
$current_time = idate('H');
$current_day = idate('w');
if ($current_day === 0 || $current_day === 6 ||
$current_time < 5 || $current_time >= 10) {
echo "We’re live Monday – Friday mornings. Check back then.";
}
$current_time=idate('H');
$current_day=idate('w');
如果($current|u day==0 |$$current|u day==6 |
$current_time<5 | |$current_time>=10){
echo“我们周一至周五上午直播。请稍后再查看。”;
}
我认为您应该更改If
语句结构,使其看起来像这样
IF (on-air)
THEN DisplayPlayer()
ELSE echo "message saying Off Air"
通过这样做,当你在线时有人试图访问该网站时,你可以更快地击中玩家
这是一种优化,当您在空中时,可以加快加载时间。如果您在非空中时,将经历两个步骤。这取决于您希望在哪里加快加载时间
除了@glavić在其回答中所写的内容外,还应使用此选项。欢迎使用StackOverflow!我建议您确切说明“它不起作用”的内容“意思是。您是否收到错误消息?否则社区成员将无法帮助您,并且可能会相应地否决您的问题。我认为,您也应该将您的或更改为|
。升级投票:您错过了一个非常明显的(效率)缺陷,需要启发吗?我对php还是比较陌生,比较字符串比比较整数效率低,即使是使用php:)($current\u time=10)
应该是($current\u time=10)
注意,他说表演在早上5点开始。完全是因为有了它。如果是早上5点,时间是5点。这会使它落入else条件,这是不需要的。对于#3,很可能是“linktoplayer.html”
也会导致解析错误。随信附上,一个字母的人!你宁愿写一条评论也不愿点击编辑,然后修改一个字符?
$current_time = idate('H');
$current_day = idate('w');
if ($current_day === 0 || $current_day === 6 ||
$current_time < 5 || $current_time >= 10) {
echo "We’re live Monday – Friday mornings. Check back then.";
}
IF (on-air)
THEN DisplayPlayer()
ELSE echo "message saying Off Air"