Home > Articles

Date and Time

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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);
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.

  • + Share This
  • 🔖 Save To Your Account