PHP script for the Christmas calendar

I am creating a PHP script for the Christmas calendar, which should only load relevant content (some text and images) based on the current (December) date.

For example, on December 1 (based on a PHP script), only the specified content should be displayed. December 2, you must specify specific content for this day / date.

My problem is to ensure that in Germany we obviously have a different time zone than in (important to me) Vancouver, Canada.

How can I get the script to work with the correct request / check when loading for the time zone / date that in these two specific time zones the content will be displayed at any time (while this, for example, is December 1 in one of these time zones)

+5
source share
2 answers

This answer is based on the assumption that you are using a web page to display the user's time zone.

First of all, you cannot get the user's timezone in PHP. But what you can do is tell the server that the user date is via JavaScript.

You can do something like this:

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 not even december in your timezone :-o } 

In timezone.php you can now determine what day is.

As a supplement:
You can set the time zone in $_SESSION so that you can set it only once.

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 the first of December echo 'I\'m shown December first'; } else if($_SESSION['user_timezone']===2){ // if it the second of December echo 'I\'m shown December second'; } // etc... 

Now you can use $_SESSION['user_timezone'] to get the time zone of users.

+2
source

Update:

In my 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 not even december in your timezone :-o } </script> 

Later in index.php:

...

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

...

In my timezone.php (in the same folder):

 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 the first of December echo 'Im shown December first';} else if($_SESSION['user_timezone']===2){ // if it the second of December echo 'Im shown December second';} 
0
source

Source: https://habr.com/ru/post/1235492/


All Articles