Problem checking if a date comes after another date


	$today_array = explode('-',"06-06-2011");
	$data_array = explode('-',"20-05-2011");

	$after = false;
	echo $after;
	if ($today_array[0] > $data_array[0] && $today_array[1] > $data_array[1] && $today_array[2] > $data_array[2] ||
 $today_array[0] >= $data_array[0] && $today_array[1] > $data_array[1] && $today_array[2] > $data_array[2] || 
$today_array[0] > $data_array[0] && $today_array[1] > $data_array[1] && $today_array[2] >= $data_array[2] || 
$today_array[0] > $data_array[0] && $today_array[1] >= $data_array[1] && $today_array[2] > $data_array[2] || 
$today_array[0] > $data_array[0] && $today_array[1] >= $data_array[1] && $today_array[2] >= $data_array[2] || 
$today_array[0] >= $data_array[0] && $today_array[1] > $data_array[1] && $today_array[2] >= $data_array[2] || 
$today_array[0] >= $data_array[0] && $today_array[1] >= $data_array[1] && $today_array[2] > $data_array[2] || 
$today_array[0] >= $data_array[0] && $today_array[1] > $data_array[1] && $today_array[2] > $data_array[2]
	) {
		$after = true;
	}
	echo $after;

The problem here is that nothing is echoed. I think this is why I’m having problems using the variable $after in other parts of the program. Why can’t I echo anything?

Oh. My.


<?php
function date_is_after($date, $after){
  $date   = vsprintf('%3$d-%2$d-%1$d', explode('-', $date));
  $after  = vsprintf('%3$d-%2$d-%1$d', explode('-', $after));
  return strtotime($date) > strtotime($after);
}

var_dump(
  date_is_after('25-12-2011', '26-12-2011')
); #false

var_dump(
  date_is_after('25-12-2011', '24-12-2011')
); #true

Try this, it’s still feels a bit clunky though and could do with a tweak or two.

function date_is_after($date, $after){
 return strtotime($after) > strtotime($date);
}

do it in 1 function instead of 3? (strtotime accepts d-m-Y as a standardized dateform)

EDIT: Even simpler.

Trust me to over complicate it! Cheers StarLion. :wink:

What? It’s not the best thing to look at, but it worked, so XD

Lol thank you :smiley: Do you have any clue why I couldn’t echo anything in my original code?

Because you could not read it.

Ok. Why? :smiley:

TRUE is a boolean, and doesnt have a string representation for ECHO that is readable.

if($after) { echo “True”; } would have worked.

Making code do what you want is only a small part of writing code. Once you have it working, you need to tweak and improve it. These improvements should not only improve it’s function, but how it reads and how understandable it is.

For example, here’s a quick (untested) rewrite or your original function.


<?php
function date_is_after($date, $after){

  $date   = explode('-', $date);
  $after  = explode('-', $after);

  $isLaterYear  = $date[2] > $after[2];
  $isLaterMonth = $date[1] > $after[1];
  $isLaterDay   = $date[0] > $after[0];

  return $isLaterYear || $isLaterMonth || $isLaterDay ? true : false ;
}

Not… quite Anthony.
2010-04-12 vs
2010-03-21

returns TRUE; (isLaterDay = TRUE), when it should return false.

But the readability factor is a valid point.

I was sticking to the format provided, and did indicate it was untested. :wink:

Working off the top of my head, sticking (roughly) to the format…


function date_is_after($date, $after){
  $date   = explode('-', $date);
  $after  = explode('-', $after);
  $year = strcmp($date[2],$after[2]);
  $month = strcmp($date[1],$after[1]);
  $day = strcmp($date[0],$after[0]);
  if($year != 0) { return (($year < 0) ? true : false); }
  if($month != 0) { return (($month < 0) ? true : false); }
  if($day != 0) { return (($day < 0) ? true : false); }
  return false; //Dates are identical.
}

Basically you need a trinary system (<,>,=) to compare accurately.

I didn’t know this ^^

I’m still in the phase highlighted in bold, I need time to evolve to the next phase :smiley: