0

Ive only ever done absolute comparisons before, so Im a bit stuck as to how to deal with this...

I have two strings returned from PHP (format is DATE_ATOM i.e 2012-01-20)

What I need to do is compare one string (date) against another - however, I need to return true on the following three conditions

  • The first date matches the second date (== got this one...)
  • The first date matches the second date +1 day
  • The first date matches the second date +2 days

Anything over that will return false.

How can I do this in as 'clean' a way as possible..

P.S This can be done in either PHP or Javascript - just the cleanest way possible would be prefered!!!

Many thanks in advance!

1
  • Are you using the full DATE_ATOM format? i.e. 2012-01-20T01:02:03+00:00 Commented Jan 23, 2012 at 10:54

5 Answers 5

1

$first = strtotime($yourFirstDate);
$second = strtotime($yourSecondData);
if($first == $second OR $first == strtotime($yourSecondDate, "+1 day") OR $first == strtotime($yourSecondDate, "+2 days")) {
   echo "ok with date";
}
else {
  echo "out of range";
}

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, clean and simple code. Many thanks. PHP is deffo the way to do this. I dont want to be splitting any variables in JS. Thanks :)
You have specified the strtotime arguments in the wrong order! it should be strtotime("+1 day", $yourSecondDate)
0

You can find the answer on php.net:

Object oriented style

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Procedural style

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');

The above examples will output:

+2 days

Source: http://www.php.net/manual/en/datetime.diff.php

Comments

0

JavaScript:

var day = 86400000;  // milliseconds (1000 * 60 * 60 * 24)

if ( Math.abs(new Date('2012-01-20') - new Date('2012-01-22')) <= 2 * day) {
  // dates are within 2 day of each other
}

Note that IE prior to version 8 does not like new Date('2012-01-20') and prefers new Date('2012/01/20') instead.

Comments

0

This is a short way of doing it:

$datetime1 = new DateTime('2012-03-20');
$datetime2=new DateTime('2012-03-18');

$intervalo = $datetime1->diff($datetime2);
$diferencia=$intervalo->format('%R%d');

if($diferencia>=0 && $diferencia<=2)
{
echo "OK";
}

The "%R" is important because it gives you if the difference is negative or positive, we only need positive, I have seen solutions that gives you OK but with negative values too.

Comments

0

For javascript you can do this way:

function compareDates(date1, date2)
{
    date1 = date1.split('-').join('');
    date2 = date1.split('-').join('');

    if (date1 == date2)        {return true;}
    if (date1 == (+date2) + 1) {return true;}
    if (date1 == (+date2) + 2) {return true;}

    return false;
}

console.log( compareDates('2012-01-20', '2012-01-20') ); // true
console.log( compareDates('2012-01-21', '2012-01-20') ); // true
console.log( compareDates('2012-01-22', '2012-01-20') ); // true
console.log( compareDates('2012-01-23', '2012-01-20') ); // false


EDITED A
date can be now either in the format '2012-01-20T01:02:03+00:00' or '2012-01-20', thanks :) ..
I don't know which format the user will use

function compareDates(date1, date2)
{
    date1 = date1.substring(0,10).split('-').join('');
    date2 = date2.substring(0,10).split('-').join('');

    if (date1 == date2)        {return true;}
    if (date1 == (+date2) + 1) {return true;}
    if (date1 == (+date2) + 2) {return true;}

    return false;
}

console.log( compareDates('2012-01-20T01:02:03+00:00', '2012-01-20') ); // true
console.log( compareDates('2012-01-21', '2012-01-20') ); // true
console.log( compareDates('2012-01-22', '2012-01-20T01:02:03+00:00') ); // true
console.log( compareDates('2012-01-23T01:02:03+00:00', '2012-01-20T01:02:03+00:00') ); // false

1 Comment

If he's really using DATE_ATOM (and not the date he provided in the question) this will not work. DATE_ATOM format is like: 2012-01-20T01:02:03+00:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.