PHP

Calculating a Relative Date

echo 'The license you have just bought is valid
  till ';
$expiry = time() + 30 * 24 * 60 * 60; //30 days)
echo strftime('%c', $expiry);

Why was 2000 a leap year, but 2100 not? The definition says: If a year is divisible by 4 and is either not divisible by 100 or is divisible by 400, it is a leap year and February has 29 days (this is because the Earth needs approximately 365.25 days to revolve around the sun). If you want to determine whether a given year is a leap year, this function comes in handy:

function isLeapYear($year) {
  return ($year % 4 == 0 &&
    ($year % 100 != 0 || $year % 400 == 0));
}

Sometimes, you have the task of calculating a date that is relative to the current date, for example, "30 days from now." Of course, you could put some real work into actually calculating this value, taking into account which day it is, whether it's a leap year, and whether DST is relevant.

Far easier is the use of an epoche time stamp. Take, for instance, the aforementioned task of finding a date that lies 30 days in the future. One day has 24 hours, one hour has 60 minutes, and one minute has 60 seconds. Therefore, to get the current time stamp (using time() or date('U')), you just need to add 30 * 24 * 60 * 60, and you have the time stamp of the desired date. This time stamp can then be used to set a cookie's expiry date or just to print out some information about this date.

Creating a Sortable Time Stamp

function timestamp($t = null) {
  if ($t == null) {
    $t = time();
  }
  return date('YmdHis', $t);
}

Sometimes, using date values with a database is not a clever thing. Different language versions of the database, its drivers, or the underlying operating system could cause some trouble when the regional date formats do not fit together.

Converting a Date into a (Sortable) Time Stamp
<?php
  function timestamp($t = null) {
    if ($t == null) {
      $t = time();
    }
    return date('YmdHis', $t);
  }
  echo 'Time stamp: ' . timestamp(time());
?>

A potential alternative is the use of time stamps. Several different formats are available, but most of them have the following structure: year-month-day-hours-minutes-seconds. Using this value order, the string representation of the date can be easily sorted, which allows using it in databases.

To create such a time stamp, just a special call to date() is required. In the preceding code, this is encapsulated in a function for easy reuse.

This format is also used by MySQL for the representation of its TIMESTAMP data type.

by BrainBellupdated
Advertisement: