- Using Text Within date()
- Automatically Localizing Dates
- Manually Localizing Dates
- Using the Current Date the U.S./U.K./European Way
- Formatting a Specific Date
- Validating a Date
- Calculating a Relative Date
- Creating a Sortable Time Stamp
- Converting a String into a Date
- Determining Sunrise and Sunset
- Using Date and Time for Benchmarks
- Using Form Fields for Date Selection
- Create Self-updating Form Fields for Date Selection
- Calculating the Difference Between Two Dates
- Using GMT Date/Time Information
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.