Dating problem<

liunx

Guest
Hi

I am having a problem calculating with dates. Specifically I am trying to work out the amount of time, in days, that has passed between two dates. Any hints to help would be appreciated.and I thought this was a question about getting a date :P

anyway, what code do you have so far? many way to do what you are doing.

have you tried strtotime()Two variables $startdate and $enddate - I simply wish to take one from the other to calculate the number of days between the two.

Help pleasewhat format are they in?Rather than try to build your own calendar routines you should:

- normalise the date format. ISO 8601 is inbuilt and preferred in many circles, the resultant format sorts into date order very easily: e.g. 20030922 is today.

- convert the time to 24-hour format (though you should already be using the 24-hour format anyway in an international context).

- normalise the time zone (adjusting the date if midnight is spanned) to UTC (You can't work out the difference for dates and times in different time zones). Inbuilt function.

- convert the date to the equivalent Julian day number (which is a handy count of days since some date in BC 4712). There is an inbuilt function for this too.

- subtract one JD number from the other.Originally posted by giz


- convert the date to the Julian day number (which is a handy count of days since some date in BC 4712). There is an inbuilt function for this too.
if it is the the start of EPOCH then it is Janruary 1 1970 or 69, I forget.Originally posted by scoutt
if it is the the start of EPOCH then it is Janruary 1 1970 or 69, I forget.


time -- Return current UNIX timestamp
Description
int time ( void )


Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).Scoutt:

The are in the formay yyyy-mm-ddUmm, yeah, EPOCH is the number of seconds since 1970-01-01 00:00:00 UTC.

However, I was on about the Julian Day Number, which is the number of days since BC4713-Jan-01.

<!-- m --><a class="postlink" href="http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=julian+day+number&btnG=Google+SearchI">http://www.google.com/search?hl=en&ie=U ... le+SearchI</a><!-- m --> figured it was tha tafter I posted. I don't believe that php has a Julian date function. all are GMT/UTC or unix timestamp which ones of those as well.try this ianprhead

<?php
$olddate = explode("-","2003-05-31"); // old date
$newdate = explode("-", date("Y-m-d")); //todays date
if($olddate[2] > $newdate[2]){
// since the day is greater we need to subtract a month
$newmonth = ($newdate[1] - $olddate[1]) - 1;
// we must add a month to the day.
$newday = ($newdate[2] - $olddate[2]) + 30;
} else {
$newmonth = ($newdate[1] - $olddate[1]);
$newday = ($newdate[2] - $olddate[2]);
}
echo "<p>".($newdate[0] - $olddate[0]) ." years & ". $newmonth ." months & ".$newday." days old" ;
?>

it should work. let me know how it goes.The above function will not work very well, since a month isn't always 30 days. Try

$olddate = explode("-","2003-02-28");
$newdate = explode("-","2003-03-01");

and check the output.. 3 days it says, when it should say 1 day.

I have done this sort of thing before, only that I can't remember it right now. I'll try to get back to you with a better solution later.well if you get technical it doesn't do leap year either. it is a easy way to do it, never said it was fool proof. :)Dating problem? Hum... :rolleyes:

Looks like you need a matchmaker service not PHP.Oh jikes, I forgot about this one.. Well an easy way to do it is the Julian day number thing, that giz mentioned.
This is how it would look like in PHP:


<?php

// The two dates to compare
$olderDate = "2000-01-01"; // Date in the past
$newerDate = date("Y-m-d"); // Today's date

// Break the strings into arrays
$olderDateArray = explode("-", $olderDate);
$newerDateArray = explode("-", $newerDate);

// Convert the dates to Julian Day Numbers
$olderJulianDay = gregoriantojd($olderDateArray[1], $olderDateArray[2], $olderDateArray[0]);
$newerJulianDay = gregoriantojd($newerDateArray[1], $newerDateArray[2], $newerDateArray[0]);

// Get the difference between the two dates
$numberOfDays = $newerJulianDay - $olderJulianDay;

// Print the difference
echo $numberOfDays." days have passed since $olderDate.";

?>


This requires the calendar extension to be installed though, if you're on UNIX.. The Windows version comes with the calendar extension enabled.Cheers chaps, that'll do nicely.

Many Regards and happy returns.

Ian
 
Back
Top