The easiest way would be to store your dates as timestamps, then you could subtract the current timestamp with the one you've saved in a database.
The PHP function time()
returns the current timestamp – that is the number of seconds since the 1st of January 1970. You can then format a timestamp $stamp
to your liking with date('m/d/Y', $stamp)
, for example.
Aside from facilitating arithmetic operations with dates, you can display more or less information with timestamps by formatting them with date()
, as well as show different formats (July 13, 2014 vs. 07/13/2014). If you save a date as a string, e.g. "8/10/2014", it will be very complicated for you to change the format, and it won't be possible to get the correct time, for example.
Finding how long ago in days a timestamp $stamp
was to the current time is very easy:
$now = time();
$days = ($stamp - $now) / (24*3600);
Use round()
to get a full number if desired (e.g. 7.2309
would simply become 7
).