圣诞节日历的PHP脚本

圣诞节日历的PHP脚本,php,date,timezone,Php,Date,Timezone,我正在为圣诞日历创建一个PHP脚本,该脚本应该只加载基于当前(12月)日期的相应内容(一些文本和图像) 例如,在12月1日(基于PHP脚本),只应显示指定的内容。12月2日,应显示该日/日期的具体内容 我现在的问题是要确保德国的时区明显不同于加拿大温哥华(对我来说很重要) 如何让脚本在加载时区/日期时进行正确的请求/检查,以确保在这两个特定时区中内容始终可见(例如,在其中一个时区中是12月1日)?此答案基于您使用网页显示用户时区的假设 首先请注意,在PHP中无法获取用户时区。但是您可以通过Jav

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

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

我现在的问题是要确保德国的时区明显不同于加拿大温哥华(对我来说很重要)

如何让脚本在加载时区/日期时进行正确的请求/检查,以确保在这两个特定时区中内容始终可见(例如,在其中一个时区中是12月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
中,您现在可以确定一天是哪一天

作为添加:
您可以在
$\u会话中设置时区,以便只需设置一次

timezone.php:

您现在可以始终使用
$\u会话['user\u timezone']
获取用户的时区。

更新:

在我的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>

谢谢你的快速回复!现在我陷入了使用数组存储一些值的问题中,在timezone.php中创建一个函数来确定日期(因此,第一天、第二天、第三天等),并基于该结果,相应的.Christmas类(使用您的示例)应该用数组中找到的日期/值组合中的值填充。。。你知道我的意思吗/Hi@maximizeIT,检查更新。现在请注意,在12月2日,JS中的
数据
变量将包含:
我在12月2日被显示
。因此,如果你有一个
div
Christmas
它的内部值将被
替换为
我在12月2日被显示
感谢更新,但我不知道我做错了什么…它对我根本不起作用…你能分享一些代码吗?
<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>
<div class="Christmas">
</div>
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';}