Jump to content

[SOLVED] The number of days between 2 dates?


Recommended Posts

Hi,

I've found a couple of solutions on this, however everything I've written only works for certain date formats and it also messes up when the date goes over a year.

 

I have the date in the format Day/Month/year - So 28/09/1991 would be the 28th of September 1991.

Now, I need to work out the amount of days between 28/09/1991 and 18/02/1996 for example.

 

How would I do this? The data would be coming in from a database, so it would be coming in that date format, d/m/y.

 

Any help much appreciated :)

Link to comment
Share on other sites

try this

function days($date1, $date2){
$month1 = date("m", strtotime($date1));
$day1 = date("d", strtotime($date1));
$year1 = date("Y", strtotime($date1));
$month2 = date("m", strtotime($date2));
$day2 = date("d", strtotime($date2));
$year2 = date("Y", strtotime($date2));
$first = gregoriantojd($month1, $day1, $year1);
$second = gregoriantojd($month2, $day2, $year2);
$diff = abs($first-$second);
return $diff;
}

 

Now you can use your dates from the database

 

$days = days($date1, $date2);

 

Ray

Link to comment
Share on other sites

See, this is what's getting at me, everything doesn't work.

 

I'm doing;

function days($date1, $date2){
$month1 = date("m", strtotime($date1));
$day1 = date("d", strtotime($date1));
$year1 = date("Y", strtotime($date1));
$month2 = date("m", strtotime($date2));
$day2 = date("d", strtotime($date2));
$year2 = date("Y", strtotime($date2));
$first = gregoriantojd($month1, $day1, $year1);
$second = gregoriantojd($month2, $day2, $year2);
$diff = abs($first-$second);
return $diff;
}

 

Then to call it;

 

$date1 = "28/09/1992";
$date2 = "28/09/1993";
$days = days($date1, $date2);

 

And it doesn't do anything ;|

This is starting to get beyond annoying, I've spent hours trying to solve this and EVERY solution doesn't work. Where am I going wrong? :(

Link to comment
Share on other sites

This function assumes that a date with slashes in it is in the form "mm/dd/yyyy" not "dd/mm/yyyy". It would be best to pass the dates in the form "yyyy-mm-dd".

 

Ken

 

Brilliant, thanks.

It's not perfect, but I can work with that, just involves messing with user input, but I can manage that :)

 

Thanks posters <3.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.