改变夜景背景天使用php? [英] Changing Background for Night & Day using php?

查看:119
本文介绍了改变夜景背景天使用php?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在tumblr页面上工作。我有两个不同的背景的html页面,我想要DAY背景显示从早上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; ?
}


解决方案

date()将返回服务器本地日期/时间,这意味着如果您的服务器位于美国,则来自东亚或澳大利亚的用户将看到错误的背景。 >

我知道这不是你想要的,但我建议使用JS,它运行用户端而不是服务器端,因此时区不会因为脚本会得到用户的时间,而不是服务器的时间。脚本将如下所示:

  var localDate = new Date(); 
if(localDate.getHours()> 7&&& localDate.getHours()< 20){
document.bgColor =#fff;
} else {
document.bgColor =#000;
}

希望这有助!


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; ?>;
}

解决方案

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:

var localDate = new Date();
if (localDate.getHours() > 7 && localDate.getHours() < 20) {
    document.bgColor = "#fff";
} else {
    document.bgColor = "#000";
}

Hope this helps !

这篇关于改变夜景背景天使用php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆