E.g. given:
$quarter1 = 4;
$year1 = 2011;
$quarter2 = 3;
$year2 = 2012;
I need to know that the second set are in the future compared to the first set.
I did think this could be done by literally appending the quarter onto the year. e.g.
$qy1 = 20114;
$qy2 = 20123;
// $qy1 < $qy2
But I'm sure this is not a reliable way to compare the two. Is there a better, more reliable way?
Many thanks
Nothing wrong with your method, other than having to do a few string concatenations and typecasting back to int. It could fail, however, if you fail to force the values back to integers, and you're comparing years with different string lengths, e.g.:
$date1 = '9994'; // year 999, q4
$date2 = '10011'; // year 1001, q1
String 9994 will compare as larger than string 10011.
Purely as an alternative, this would work as well, keeping things purely numeric:
$date1 = $year1 + (($quarter1 - 1) / 4);
$date2 = $year2 + (($quarter2 - 1) / 4);
if ($date1 < $date2) { ... }
Yes, I do think this is the best, simplest, and quickest way to do this.
if($year1<$year2 || $year1==$year2 && $quarter1<$quarter2)
echo "Date1 is before Date2!";
else
echo "Date1 is NOT before Date2!";