【问题标题】:PHP Script for Christmas calendar [closed]圣诞日历的 PHP 脚本 [关闭]
【发布时间】:2015-11-08 20:28:44
【问题描述】:

我正在为圣诞节日历创建一个 PHP 脚本,该脚本应该仅根据当前(12 月)日期加载相应的内容(一些文本和图像)。

例如,在 12 月 1 日(基于 PHP 脚本)应该只显示指定的内容。 12 月 2 日,应显示当天/日期的具体内容。

我现在的问题是确保在德国,我们显然与加拿大温哥华(对我来说很重要)有不同的时区。

如何让脚本使用正确的请求/检查加载时区/日期,在这两个特定时区中内容始终可见(例如其中一个时区的 12 月 1 日时区)??

【问题讨论】:

    标签: php date timezone


    【解决方案1】:

    此答案基于您使用网页显示用户时区的假设。

    首先请注意,您无法在 PHP 中获取用户时区。但是你可以做的是通过 JavaScript 告诉服务器用户日期是什么。

    你可以这样做:

    var currentdate = new Date();
    if(currentdate.getMonth() === 11){ // note that January = 0, Feb = 1 etc. So December = 11
        $.get( "//christmasServer.com/timezone.php?user_day=" + currentdate.getDate(), function( data ) { // send to server, getDate() will show the day, December 14 will just show 14
          $('.Christmas').html( data ); // do something with the data, for example replace the div .Christmas
        });
    } else {
        // It's not even december in your timezone :-o
    }
    

    timezone.php 中,您现在可以确定今天是什么日子。

    作为补充:
    您可以在$_SESSION 中设置时区,这样您只需设置一次。

    timezone.php

    if(!isset($_SESSION['user_timezone'])){ // if timezone isn't set
        session_start(); // start session
        $_SESSION['user_timezone'] = $_GET['user_day'];
    }
    
    if($_SESSION['user_timezone']===1){ // if it's the first of December
        echo 'I\'m shown December first';
    } else if($_SESSION['user_timezone']===2){ // if it's the second of December
        echo 'I\'m shown December second';
    }
    
    // etc...
    

    您现在可以随时使用$_SESSION['user_timezone'] 来获取用户的时区。

    【讨论】:

    • 感谢您的快速回复!现在我被困在使用数组存储一些值的问题上,在 timezone.php 中创建一个函数来确定日期(所以第 1、第 2、第 3 天等),并根据该结果相应的 .Christmas 类(使用您的示例)应该填充从数组中找到的日期/值组合中的值......你知道我的意思吗? ://
    • 嗨@maximizeIT,检查更新。现在请注意,在 12 月 2 日,JS 中的 data 变量将包含:I'm shown December second。因此,如果您有一个具有 Christmas 类的 div,它的内部值将被替换为 I'm shown December second
    • 感谢您的更新,但我不知道我做错了什么...对我来说根本不起作用...
    • 你能分享一些代码吗?
    【解决方案2】:

    更新:

    在我的 index.php 中:

    <script type="text/javascript">
    var currentdate = new Date();
    
    if(currentdate.getMonth() === 10){ // for testing with november (=10)
        $.get( "/timezone.php?user_day=" + currentdate.getDate(), function( data ) { // correct for localhost testing version, so no domain yet?!
          $('.Christmas').html( data ); // do something with the data, for example replace the div .Christmas
        });
    } else {
        // It's not even december in your timezone :-o
    }
    </script>
    

    在 index.php 后面:

    ...

    <div class="Christmas">
    </div>
    

    ...

    在我的 timezone.php 中(在同一个文件夹中):

    if(!isset($_SESSION['user_timezone'])){ // if timezone isn't set
    session_start(); // start session
    $_SESSION['user_timezone'] = $_GET['user_day'];}
    if($_SESSION['user_timezone']===1){ // if it's the first of December
    echo 'Im shown December first';} else if($_SESSION['user_timezone']===2){ // if it's the second of December
    echo 'Im shown December second';}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2021-04-02
      • 2021-04-06
      • 1970-01-01
      相关资源
      最近更新 更多