Temporary links with PHP

One of the things that you have asked us in the comments is if you can create temporary links with PHP. That is, the link only appears on the page in a specific time slot.

Let’s see how easy it is to mount temporary links by using the function
date()
. And knowing how to handle the function
date()
of PHP we can obtain the information regarding the day and time, which is in a very simple way.

The first thing we have to know is that when handling date issues in PHP and the function
date()
specifically, we must set to the code the time zone in which the program is executed. To indicate the execution time zone we will use the function
date_default_timezone_set()
, to which we can indicate a zone such as ‘Europe/Madrid’ or a general value such as ‘UTC’. In our case we will encode the following:

date_default_timezone_set('Europe/Madrid');

The next thing we will do is code a function that we will call show() that will return a boolean value: true if the date coincides with a specific day of the week and time and false in the rest of the cases.

function show() { }

The function
date()
receives a text string in which we specify the format of the current date information that we want it to return. In the documentation of the date function we will see all the values ​​that can be passed to it.

Some of them are, for days:

  • d, day of the month in two digits.
  • D, weekly representation of the day with three letters. From Monday to Sunday.
  • j, day of the month in two digits, but without leading zeros.
  • l, textual representation of the day.
  • S, English ordinal suffix. It’s the 1st, 2nd,…
  • w, numerical representation of the week. What day of the week is it? Starting with 0 for Sunday.
  • z, day of the year starting with 0 and ending with 365.

For hours:

  • a, am/pm in lowercase.
  • A, am/pm in capital letters.
  • B, Internet time from 000 to 999.
  • g, 12-hour format without leading zeros.
  • G, 24-hour format without leading zeros.
  • h, 12-hour format with leading zeros.
  • H, 24-hour format with leading zeros.
  • i, minutes with leading zeros.
  • s, seconds with leading zeros.
  • u, milliseconds.

… and so on many more that are in the documentation for the date() function.

In our case we are going to use the modifier 'w' which gives us the day of the week and 'H' which tells us the time in 24 format.

You have to be careful since the value of 'w' starts with 0 for Sunday, 1 for Monday and ending with 6 for Saturday.

So we will code the function in the following way to show the link only on Sundays from 1:00 p.m. to 2:00 p.m.:

function show() {
  if ((date("w") == 0) && (date("H") == 13))
    return true;
  else
    return false;
}

You can add or modify the conditions you want to use in the if statement.

We will only have to invoke the function when we are going to paint the link in the following way:

if (show())
  echo 'Link On Date';

Through this mechanism we can have temporary links with PHP.