How to use the server date on the site?

How to use the server date on the site?


What is server date? Simply put, it is the date of the server where your site is located. How is this useful? Of course, the fact that you cannot just take, change the date on your computer and use the “loopholes” in the sites. Let’s say some event is on October 20, you put on the computer on October 20 and the event opens. So that the date on the computer (you can get it through the standard Date object) does not affect any data on the site – we use the server date.

How do I use the date? There are some tricks here.


First, we need to create a php file and name it, for example, date.php


$result['status'] = 'ok';
$result['server_datetimeY'] = date("Y");
$result['server_datetimeM'] = date("m");
$result['server_datetimeD'] = date("j");
$result['server_datetimeT'] = date("t");
echo json_encode($result);


I’m not a PHP expert, but everything is quite simple here. We get the server date, convert it to Json (special format).

We can see that multiple data is being generated. date('Y'), date('m') etc. In order not to think about what exactly we need – just follow the link to the PHP manual, from there you can easily take any data. link.

Next, we need to somehow process the data and operate on it. Everything is pretty simple here too. Writing jQuery code.


let year, month, day2, daysInMonth;
type: "POST",
url: "date.php",
dataType: "json",
cache: false,
success: function(data){
if (data.status == 'ok') {
year = data.server_datetimeY;
month = data.server_datetimeM;
day = data.server_datetimeD;
daysInMonth = data.server_datetimeT;
localStorage['ServeTime'] = data.server_datetimeM;

Simple request to date.php file and getting data into variables. I draw your attention to the fact that this method will work either through OpenServer, or already directly on the hosting (for PHP to work).

As a result, we have the corresponding data in the variables year, month, day and daysInMonth. Yes, in theory, you can redefine these variables from the outside (since they are global), but no one bothers you to secure yourself)

And the last thing you need to do to work with these variables directly in the code is setTimeout. At least 100 milliseconds for the data to appear after downloading from the server.

That’s all, good luck to you!

Leave a Reply

Your email address will not be published. Required fields are marked *