Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the number of days betwen two dates

Status
Not open for further replies.

rvr1

Programmer
Feb 6, 2004
16
NL
I am developing my first PHP application and would like some help writing a function to find the number of days between a date stored in a mySQL database and the current date.

If anyone could help me with this, I would appreciate it.

Thanks.
 
I have a FAQ in this forum for calculating the difference between two dates in years, months, and days: faq434-3493

Also, you could use PHP's time() function to get the seconds timetick number for the current time, use PHP's strtotime() function to convert the MySQL-provided date to a seconds timetick number, subtract the smaller of the two from the larger, and divide that result by 86400 (24 hours/day * 60 minutes/hour * 60 seconds/minute) rounding down.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
This is cribbed from the MySQL Cookbook....

Using MySQL, convert both dates to basic units and calculate the difference between the resulting values.

To calculate an interval in days between date or date-and-time values, convert them to days using TO_DAYS(), then take the difference.

Code:
mysql> SELECT (TO_DAYS('1884-01-01') - TO_DAYS('1883-06-05') AS days;

+------+
| days |
+------+
|  210 |
+------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top