Finding number of days between two dates [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Finding number of days between two dates [Solved]

Post 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:
Last edited by technofreak on Tue Jun 20, 2006 12:33 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post 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 :)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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;
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Have a look at the DATEDIFF function in DATE and TIME functions section of the MySQL manual
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post 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.
Last edited by technofreak on Mon Jun 19, 2006 4:11 am, edited 1 time in total.
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post 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.
harsh789
Forum Newbie
Posts: 4
Joined: Fri Jun 09, 2006 12:11 pm

Post by harsh789 »

You must calculate days as follows,

Code: Select all

$diff_bw_dates = (($epoch_expiry - $epoch_new_activatn)/24*60*60);
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post 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.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

better you do it with some database functon, it will be easy.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Re: Finding number of days between two dates

Post 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
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

Thanks a lot feyd and printf. Your replies where really helpful, the problem got solved :)
Post Reply