View Full Version : Subtracting date into numbers
03-08-2008, 03:17 AM
I am trying to take a date, and another date and subtract the number of days to make a number. So if you registered 01/01/08 and today is 01/05/08 it should subtract the dates and say 4 Days. So I want it to store the number of days they have been registered. Is there an easier way then this?
03-08-2008, 03:55 AM
Use mktime to convert the dates to a unix timestamp then subtract the one from the other giving you the number of seconds difference, then multiply seconds by 60*60*24 to get days.
03-08-2008, 04:38 AM
Wow I am extremely confused. I know a very good amount of php, but I am beginning to learn date/time functions. Let me go to php.net and look at this mktime function. Thanks
03-08-2008, 05:12 AM
Assuming the date was in mm/dd/yy format you would:
list($month, $day, $year) = explode('/', $date);
$difference = time() - mktime(0, 0, 0, $month, $day, $year);
$days = floor($difference / (60 * 60 * 24));
If you had the date stored in MySQL's DATE/DATETIME format you could use the MySQL date functions and it would be a lot easier.
03-08-2008, 05:22 AM
Yeah I have it stored in mysql in a DATE field. I just don't know how to change it to show days after 24 hours.
03-08-2008, 05:45 AM
The mysql datediff() function directly returns the number of days between two DATE or DATETIME values - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_datediff
No slow php code is necessary.
03-08-2008, 01:22 PM
Thats useful and I read it over and over and can't understand. I am beginning to think im a failure with php. I try so hard to learn this complex stuff.
03-08-2008, 07:12 PM
This will return a column called "days_registered":
SELECT DATEDIFF(NOW(),`date`) AS `days_registered` FROM `table` WHERE 1;Replace "date" with the date field you're using.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.