View Full Version : determine length of time to date
03-04-2007, 04:38 PM
Hello all, I'm trying to figure out how many days there are to an annual date (e.g., I need to know whether I'm approaching a yearly invoice date). The approach seems simple, but I've not been able to figure out this 'logic' problem. I assume this task needs to account for the year of the invoice date and today's date. Then it seems as though I just need to determine the number of days between those two dates once the year has been taken out of the equation.
If anyone could be of assistance, I'd apprecdiate it.
03-04-2007, 05:08 PM
So long as your dates are within a range that can be represented using a UNIX timestamp (13 Dec 1901 (or 01-01-1970 on some op sys/php ver) to 19 Jan 2038), the easiest way is to get both the start and end date in terms of a timestamp (seconds), subtract the two to give the number of seconds difference, and do the division to give the number of days this corresponds to.
03-04-2007, 05:13 PM
Ok, I should have been more clear with my objective. In my admin area, I enter the creation date for a client. This date includes the month, day, and year of the creation date. I want this creation date to also serve as an annual reminder of when to invoice each client. For example, with a creation date of March 3, 2005, I want to receive a reminder to invoice that client EVERY year 30 days prior to March 3 even though the creation/invoice date does not change.
03-04-2007, 05:22 PM
You need to be even more specific, what format are you storing the date as and is this in a database (if so what kind.)
03-04-2007, 05:27 PM
Sorry, currently, dates are stored in the following format: YYYY-MM-DD
If need be, I can always store this date in the timestamp format.
03-04-2007, 08:16 PM
So as I see it, the problem lies in the fact that the creation/invoice date is a static value derived from a database. Thus, the year on this date never changes. However, time moves on...and the year of the current date increases.
Simply put, my question is how do I disregard year and determine the number of days between now and the creation/invoice date? For example, how many days are between March 4 and October 1 (of no specific year)?
03-04-2007, 09:04 PM
This will tell you how many days are left until the next invoice date:
$datetime = '2007-03-03';
list(,$month, $day) = explode('-', $datetime);
$days_left = (mktime(0,0,0,$month, $day, date('Y')) < time()) ? round((mktime(0,0,0,$month, $day, date('Y') + 1) - time()) / (60 * 60 * 24)) : round((mktime(0,0,0,$month, $day, date('Y')) - time()) / (60 * 60 * 24));
03-04-2007, 09:14 PM
Thanks so much! That code worked perfectly! As I suspected, this was a little beyond my skill level, but I have definitely learned from your code.
Thanks for helping to keep this such a great forum!
03-04-2007, 11:55 PM
You're very welcome. =]
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.