I noticed date_difference() always return an absolute value, but I needed to compare two dates to know which one is greater, so I wrote the following:

/**
 * Compare two dates.
 *
 * @param mixed $date1
 *   the starting date
 * @param mixed $date2
 *   the ending date
 * @param string $type
 *   the type of dates provided:
 *   DATE_OBJECT, DATE_DATETIME, DATE_ISO, DATE_UNIX, DATE_ARRAY
 * @return
 *   -1 if the first date is lower than the second, 0 if they are equal,
 *   and 1 if the second is lower.
 */
function date_compare($date1_in, $date2_in, $type = DATE_OBJECT) {
  // Create cloned objects to make sure original dates are not modified.
  $date1 = drupal_clone(date_convert($date1_in, $type, DATE_OBJECT));
  $date2 = drupal_clone(date_convert($date2_in, $type, DATE_OBJECT));
  $diff = date_format($date1, 'U') - date_format($date2, 'U');
  return ($diff > 0 ? 1 : ($diff < 0 ? -1 : 0));
}

Maybe this could be added to the Date API? If we cannot add more functions to the API, then I hope this saves a bit of time to someone else out there.

Comments

markus_petrux’s picture

Here's a version that requires the caller to convert the dates, if necessary.

This function kind of mimics version_compare().

/**
 * Compare two dates.
 *
 * Examples:
 *
 * @code
 * if (date_compare($date1, $date2) > 0) {
 *   // $date1 is greater than $date2.
 * }
 * if (date_compare($date1, $date2) < 0) {
 *   // $date1 is lower than $date2.
 * }
 * if (date_compare($date1, $date2) == 0) {
 *   // $date1 is equal to $date2.
 * }
 * @endcode
 *
 * @param mixed $date1
 *   the starting date
 * @param mixed $date2
 *   the ending date
 * @return
 *   -1 if the first date is lower than the second, 0 if they are equal,
 *   and 1 if the second is lower.
 */
function date_compare($date1, $date2) {
  $diff = date_format($date1, 'U') - date_format($date2, 'U');
  return ($diff > 0 ? 1 : ($diff < 0 ? -1 : 0));
}

I'm using this function to compare dates to grant/deny access to nodes based on workflow rules defined by custom code. I hope this function can be useful to other as it is a bit more intuitive than doing the inner logic in it explicitly everywhere you need this kind of comparisons.

@Karen: If you like the idea, then please let me know if you want it in patch form. I thought it could be placed just above date_difference() code. Also, the README would have to be updated. Not sure if there would be more stuff to update.

chill8331’s picture

Thank you very much! This is exactly what i'm looking for!!! Maybe the date_difference function need a little bit improve. Merge these two function to one.

arlinsandbulte’s picture

Status: Active » Postponed (maintainer needs more info)

Does such a short & simple block of code deserve its own function?
Why should we add another function to the date api?

markus_petrux’s picture

@arlinsandbulte: it makes your code more readable, and it also helps to remind how to perform such operations, which is easier with this function because it mimics version_compare(). But well, this is just a personal point of view. YMMV.

karens’s picture

Status: Postponed (maintainer needs more info) » Needs review

This function was requested a long time ago, I suspect there's an old issue about it somewhere. It's a simple thing to add and I can see the usefulness. @chill8331, I don't think we want to munge this into the date_difference function, they are achieving two different goals.

I have no objection to adding this, but no time to do anything with it right now.

arlinsandbulte’s picture

OK,
Here is a patch of the original post (handles date conversion). If you think #1 should be used instead, let me know and I will make a patch for that.
I did some really quick tests and it seems to work ok (I did not get any fatal errors at least).

Karen, if you could just do a quick once-over, I could get it committed.

Once that happens, it should be added to the D7 branch too. The only thing that needs to be changed for D7, I think, is to replace drupal_clone() with just clone() as D7 does not support PHP4. Does that sound about right?

markus_petrux’s picture

Good point in adding format conversion, though maybe it could be done only when the $type argument is not DATE_OBJECT, for performance reasons.

Thanks for taking into consideration! :)

alb’s picture

If I have two fields, start date and end date, is this the code (a part of code) to use for does this:
after insert both date code verify if start date is greater than current date and if end date is grater than
start date; if not the node cannot to saved;
which is the solution?

damienmckenna’s picture

Issue summary: View changes
Status: Needs review » Closed (won't fix)

Unfortunately the D6 version of this module is no longer supported, but we appreciate the time you put into this. If this problem is relevant for D7 too, please reopen the issue. Thanks.