Page 1 of 1

Finding number of days between two dates [Solved]

Posted: Mon Jun 19, 2006 1:44 am
by technofreak
Hey,

I am designing a client account management application and I need to provide a feature to update the account validity dates when the user renews his account.

I have a $activation_date, $validity and $expiry_date which hold the values fetched from the database. These are values of current subscipription details. Now, using another variable $validity_increment, I increase the valididty and calculate a new expiry date corresponding to the renewal.

Taking the case, where the renewal is made a few days before the existing expiry date, how will i find the new validity, which is logically,

Code: Select all

$validity_new = ($expiry_date_new - $expiry_date_old) + $vaidity_increment;
Is there a way to find the number of days between any two date values, where the dates are in the form 'mm/dd/yy' ? :roll:

Posted: Mon Jun 19, 2006 1:50 am
by feyd
Not without some conversion or using a database's functionality. As I've said before, look through Code Snippets. There was a snippet posted some time ago that makes the difference fairly simple to calculate.

Posted: Mon Jun 19, 2006 2:20 am
by technofreak
Feyd,

There is one post in 'code snippets' under the title "Diff (using UNIX cmdline diff util)" but it doesn't deal with finding the difference between two dates.. I wasn't able to find anything similar to my requirement. If there is a code snippet, can anyone point me to that ? :oops:

I will also try to come up with a code to solve my problem :)

Posted: Mon Jun 19, 2006 2:44 am
by Luke
I usually store all of my dates as unix time stamps (amount in seconds since December 31st 1969 I believe) in a database... it's so much easier to work with dates this way. But if you must store the dates in the database as they are, just convert them to timestamps either with strtotime() like so:

Code: Select all

$timestamp = strtotime("03/10/06"); // strtotime converts almost any human interpretation of a date into a unix timestamp... pretty cool, but be careful because it  does some funky things some times
Or with mktime and explode like this:

Code: Select all

$date_array = explode("/", "03/10/06"); // Uses arg one as a dilimiter and returns an array
$month = $date[0];
$day = $date[1];
$year = $date[2] + 2000; // Add 2000 since year is stored in two digits

$timestamp = mktime ( 0, 0, 0, $month, $day, $year);
Then it's just a matter of the logic you provided to begin with
compare the timestamps to see if they are two days (24 hours) apart

Code: Select all

// First you need to know how many seconds are in 24 hours
$twenty_four_hours = 24 * 60 * 60; // 60 seconds times 60 minutes * 24 hours

$validity_new = ($timestamp2 - $timestamp1) + $twenty_four_hours;

Posted: Mon Jun 19, 2006 3:12 am
by JayBird
Have a look at the DATEDIFF function in DATE and TIME functions section of the MySQL manual

Posted: Mon Jun 19, 2006 3:42 am
by technofreak
I coded similar to this,

Code: Select all

$epoch_expiry = strtotime($expiry_date_old);
$epoch_new_activation = strtotime(date('m/d/y'));
$validity_incr = $_POST['validity_increase'] * 60 * 60;

$valdiity_new = ($epoch_new_activation - $epoch_expiry) + $validity_incr;
This should give me the new validity period from 'today' till the new expiry date, assuming that the renewal is made before the old expiry date.

Posted: Mon Jun 19, 2006 4:10 am
by technofreak
Sorry,

Soemthing seems to be getting wrong. Am not getting the right number of days. Here's my code...

Code: Select all

<?php

//print $date_between;

$ending_day = strtotime("09/28/06");
$activatn_date = date('m/d/y');

$epoch_expiry = $ending_day;
$epoch_new_activatn = strtotime($activatn_date);
$increment_validity = 100 * 60 * 60;
$diff_bw_dates = strftime("%d", ($epoch_expiry - $epoch_new_activatn));
$validity_new = ($epoch_expiry - $epoch_new_activatn) + $increment_validity;
$validity_in_days = strftime("%d", $validity_new);

print "end date" .$epoch_expiry ." <br /> ";
print "activation new" .$epoch_new_activatn ." <br /> ";
print "diff between dates" .$diff_bw_dates ." <br /> ";
print "increment new" .$increment_validity ." <br /> ";
print "new validity" .$validity_new. " <br /> ";
print "in days " .$validity_in_days;
?>
Here is the output from the browser...

Code: Select all

end date1159381800
activation new1150655400
diff between dates12
increment new100
new validity8726500
in days 16
Though there is a difference of 101 days between the $ending_day and the $activatn_date, the expression " strftime("%d", ($epoch_expiry - $epoch_new_activatn))" outputs only 16 days, and hence the final result also has an error as it shows 12 days even after adding an 100.

Posted: Mon Jun 19, 2006 6:03 am
by harsh789
You must calculate days as follows,

Code: Select all

$diff_bw_dates = (($epoch_expiry - $epoch_new_activatn)/24*60*60);

Posted: Mon Jun 19, 2006 6:43 am
by technofreak
That needs a bit of change,

Code: Select all

$diff_bw_dates = (($epoch_expiry - $epoch_new_activatn)/(24*60*60));
Btw, the values returned by strtotime() and mktime() of a same time-stamp value gives (gave in my case) different results. Why so ? I got the result correctly byusing mktime() after expoding the date in the "m/d/y/' format. But when i tried to do the same with strtotime('m/d/y') then the results were nowhere near the ideal values, the former was some 101 days ( ideal value was 100 days) and the later was some 2524 days.

Posted: Mon Jun 19, 2006 7:00 am
by itsmani1
better you do it with some database functon, it will be easy.

Posted: Mon Jun 19, 2006 10:03 am
by feyd
I was referring to this: viewtopic.php?t=29341

You must first calculate the time difference in seconds, which is simple enough with mktime(), strtotime(), et al.

Re: Finding number of days between two dates

Posted: Mon Jun 19, 2006 11:36 am
by printf
technofreak wrote:Is there a way to find the number of days between any two date values, where the dates are in the form 'mm/dd/yy' ? :roll:

Code: Select all

<?

$date_a = strtotime ( '09/28/06' );
$date_b = time ();


echo ceil ( ( ( $date_a - $date_b ) / 86400 ) ); 

?>

You can add GMT offset to, if your timezone is +13 because using ceil() will not effect hours for -12 to +12, but it will for timezone +13, so add that to the subtracting before the / to handle that time zone correctly! (only if your using that time zone)


pif

Posted: Tue Jun 20, 2006 12:33 am
by technofreak
Thanks a lot feyd and printf. Your replies where really helpful, the problem got solved :)