改变夜晚和夜晚的背景天用php吗?

发布于 2024-10-06 06:09:03 字数 451 浏览 1 评论 0原文

我正在制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

杯别 2024-10-13 06:09:03

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

我知道这不是您想要的,但我建议使用 JS 来实现,它在用户端而不是服务器端运行,因此时区并不重要,因为脚本将获取用户的时间,而不是服务器的时间。该脚本看起来像:

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

希望这有帮助!

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 !

善良天后 2024-10-13 06:09:03

尝试将:更改

body {
    background: <?php echo $bg; ?>;
}

为:

<body style="background-color: <?php echo $bg; ?> !important;">

You PHP Code 对我来说似乎没问题。请注意,中午 12:00 至午夜 12:00 期间将显示白色背景。尝试使用以下代码,从上午 7:00 到晚上 8:00 保持白色背景,其他时间保持黑色(正如 Aaron 所说)。

替换:

if ($now < 12) {
    $bg = '#FFFFFF';
} else {
    $bg = '#000000';
}

为:

if ($now < 7 || $now > 20) {
    $bg = '#000000';
} else {
    $bg = '#FFFFFF';
}

Try changing:

body {
    background: <?php echo $bg; ?>;
}

to:

<body style="background-color: <?php echo $bg; ?> !important;">

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:

if ($now < 12) {
    $bg = '#FFFFFF';
} else {
    $bg = '#000000';
}

with:

if ($now < 7 || $now > 20) {
    $bg = '#000000';
} else {
    $bg = '#FFFFFF';
}
子栖 2024-10-13 06:09:03

您使用的 date 函数返回当前时间的 24 小时格式。接下来,在条件中您应该修改它以检查您所在的范围。如果 $now 介于 7 和 20 之间,您可以将其设置为白天背景。否则,将其设置为夜间。如果您需要更多详细信息来执行此操作,请告诉我。

<?php
header('content-type: text/css');

date_default_timezone_set("America/Vancouver");
$now = date('G');

if ($now > 7 && $now < 20) {
    $bg = '#FFFFFF';
} else {
    $bg = '#000000';
}
?>

<body style="background-color:<?php echo $bg; ?> !important;">
 <!-- Your HTML code here -->
</body>

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.

<?php
header('content-type: text/css');

date_default_timezone_set("America/Vancouver");
$now = date('G');

if ($now > 7 && $now < 20) {
    $bg = '#FFFFFF';
} else {
    $bg = '#000000';
}
?>

<body style="background-color:<?php echo $bg; ?> !important;">
 <!-- Your HTML code here -->
</body>
早乙女 2024-10-13 06:09:03

: 中使用了类似的东西

your php

    function getUKTime() {

    date_default_timezone_set('Europe/London');

    $time = date('H');
    $style = "";

    if ($time >= 6 && $time <= 18) {
        $style = $style . "day";
    }
    else {
        $style = $style . "night";
    }

    echo $style;
}

我在你的 html

<div id="uk" class="<?php getUKTime(); ?>">
    <h1>U.K.</h1>
</div>

: then just style 你的 .night 或 .day 类

I used something like this

your php:

    function getUKTime() {

    date_default_timezone_set('Europe/London');

    $time = date('H');
    $style = "";

    if ($time >= 6 && $time <= 18) {
        $style = $style . "day";
    }
    else {
        $style = $style . "night";
    }

    echo $style;
}

in your html:

<div id="uk" class="<?php getUKTime(); ?>">
    <h1>U.K.</h1>
</div>

then just style your .night or .day classes

凉城 2024-10-13 06:09:03

您可以设置不同的 css 文件,一个用于白天,另一个用于夜间。

$date = getDate();

// check day or night
// it's day by default
$css = "css/main.css";

if ($date['hours'] < 7 || $date['hours'] > 20){
    // is night
    $css = "css/main-night.css";

}

然后,在 标记中定义页面的样式文件。

    <link href="<?= $css ?>" rel="stylesheet">

You can set different css files, one for day time and the other for the night.

$date = getDate();

// check day or night
// it's day by default
$css = "css/main.css";

if ($date['hours'] < 7 || $date['hours'] > 20){
    // is night
    $css = "css/main-night.css";

}

Then, in the <meta> tag, that define the page's style file.

    <link href="<?= $css ?>" rel="stylesheet">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文