How do I Create a Date Range Using PHP?

PHP does not have a built-in date range function, but a programmer can create his own using a combination of other functions. He can prompt the user to enter dates and then format and manipulate the data as needed.

  1. Functions

    • The date function returns a formatted date string and the mktime function returns a numerical value from time and date parameters. A programmer can write a function that accepts two dates, putting them into two mktime functions, calculate the difference and use the date function to format the output. For example, "date('j', (mktime(1, 0, 0, 10, 27, 2010) - mktime(1, 0, 0, 10, 05, 2010)));" calculates that 22 days exist between Oct. 5, 2010 and Oct. 27, 2010.

    Benefits

    • A date range function has many applications. For example, a date range function can keep track of a start and end date, such as those used by rental companies, or it can calculate the number of days that occur between two given dates, such as a program that counts down from the current day to a selected date.

    Considerations

    • When creating a date range function, using a "YYYY-MM-DD" or "YYYYMMDD" date format may work best as that format is compatible with other programs such as MySQL. Using a compatible format allows the programmer to incorporate his PHP date range function into other programs and computer languages.

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured