I'm creating a PHP script for a Christmas calendar which is supposed to only load the respective content (some text and images) based on the current (December) date.
For example, on the 1st December (based on the PHP script) only the specified content should be shown. On the 2nd December, the specific content for that day/date should be shown.
My problem now is to ensure that in Germany we, obviously, have a different time zone than in (for me important) Vancouver, Canada.
How can I get the script working with the right request/checks on loading for the timezone/date that in these two particular timezones the content is visible at all times (while it is for instance the 1st of December in one of these timezones)??
This answer is based on the assumption that you use a webpage to show the user timezone.
First note that you can not get the user timezone in PHP. But what you can do is tell the server what 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's not even december in your timezone :-o
}
In timezone.php
you can now determine what the day is.
As addition:
You can set the timezone in the $_SESSION
so that you only have to set it 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'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...
You can now always use $_SESSION['user_timezone']
to get the users' timezone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With