GThamizh GThamizh - 5 months ago 18
PHP Question

How do I Find Range Values Present On Another Range Values

Find the range values between another range values,
Below code is working fine, But i need any other optimized way.

<?php
$lmi = 1000;
$lmx = 20000;
$cmi = 19000;
$cmx = 40000;
$cnt = count(array_intersect(range($lmi,$lmx),range($cmi,$cmx)));
print_r($cnt);
?>


If anyone find the solution please share to me..Thanks advance

Answer Source

If you are trying to compute the amount of overlap, try:

$overlap = ($lmx < $cmi || $cmx < $lmi) ? 0 : min($lmx-$cmi, $cmx-$lmi);
print_r($overlap);

This returns zero if there is no overlap and the amount of overlap otherwise.