改变夜晚和夜晚的背景天用php吗?
我正在制作一个 tumblr 页面。我的 html 页面有两种不同的背景,我希望白天背景从早上 7 点到晚上 8 点显示,夜间背景从晚上 8 点到早上 7 点显示。
我决定用 php 来做这件事,但对于 php 来说我是个新手。我的朋友给我发了一个我可以使用的示例代码。但我不知道该怎么办。你们能帮忙吗?
这是示例代码。
<?php
header('content-type: text/css');
date_default_timezone_set("America/Vancouver");
$now = date('G');
if ($now < 12) {
$bg = '#FFFFFF';
} else {
$bg = '#000000';
}
?>
body {
background: <?php echo $bg; ?>;
}
I'm working on a tumblr page. I have two different backgrounds for the html page and I want the DAY background to display from 7am to 8pm and the night background to display from 8pm to 7am.
I decided to do this in php, but i'm a total newb when it comes to php. My friend sent me an example code that I could use. But I have no idea what to do with it. Can you guys help.
Here's the example code.
<?php
header('content-type: text/css');
date_default_timezone_set("America/Vancouver");
$now = date('G');
if ($now < 12) {
$bg = '#FFFFFF';
} else {
$bg = '#000000';
}
?>
body {
background: <?php echo $bg; ?>;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
date()
将返回服务器本地日期/时间,这意味着如果您的服务器位于美国,来自东亚或澳大利亚的用户将看到错误的背景。我知道这不是您想要的,但我建议使用 JS 来实现,它在用户端而不是服务器端运行,因此时区并不重要,因为脚本将获取用户的时间,而不是服务器的时间。该脚本看起来像:
希望这有帮助!
The
date()
will return the server local date/time, which means that if your server is in the US, a user from Eastern Asia or Australia for example, will see the wrong background.I know this isn't what you were looking for, but I would suggest doing it with JS, which runs user-side rather than server-side, and therefore the timezone will not matter because the script will get the user's time, not the server's time. The script would look something like:
Hope this helps !
尝试将:更改
为:
You PHP Code 对我来说似乎没问题。请注意,中午 12:00 至午夜 12:00 期间将显示白色背景。尝试使用以下代码,从上午 7:00 到晚上 8:00 保持白色背景,其他时间保持黑色(正如 Aaron 所说)。
替换:
为:
Try changing:
to:
You PHP Code seems to be fine to me. Note that, it will display white background from 12:00 noon to 12:00 midnight. Try the following code for keeping white background from 7:00 AM to 8:00 PM and black other times (as Aaron said).
Replace:
with:
您使用的
date
函数返回当前时间的 24 小时格式。接下来,在条件中您应该修改它以检查您所在的范围。如果$now
介于 7 和 20 之间,您可以将其设置为白天背景。否则,将其设置为夜间。如果您需要更多详细信息来执行此操作,请告诉我。The
date
function you're using is returning the 24 hour format of the current hour. Next, in the conditional you should modify that to check what range you're in. If$now
is between 7 and 20 you can set it to the daytime background. Otherwise, set it to the night time. Let me know if you need more details for doing this.: 中使用了类似的东西
your php
我在你的 html
: then just style 你的 .night 或 .day 类
I used something like this
your php:
in your html:
then just style your .night or .day classes
您可以设置不同的 css 文件,一个用于白天,另一个用于夜间。
然后,在
标记中定义页面的样式文件。
You can set different css files, one for day time and the other for the night.
Then, in the
<meta>
tag, that define the page's style file.