圣诞节日历的PHP脚本

max*_*eIT 5 php timezone date

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

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

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

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

Bob*_*ijt 2

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

首先请注意,您无法在 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
}
Run Code Online (Sandbox Code Playgroud)

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

另外:
您可以在 中设置时区,$_SESSION这样您只需设置一次。

时区.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...
Run Code Online (Sandbox Code Playgroud)

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