3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $arr_dates = array(20150616,20150615,20150612,20150611,20150610,20150609,20150608,20150605,20150604,20150603,20150602,20150601,20150529,20150528,20150521,20150514,20150507,20150430,20150423,20150416,20150409,20150402,20150326); $current_date = 20150616; $expected_trend_date = 20150516; $expected_result = 20150514; $trend_index = 0; $last_diff = 100000; foreach ($arr_dates as $index => $date) { $diff = abs($date - $expected_trend_date); echo $diff, ':', $date, PHP_EOL; if ($diff < $last_diff) { $last_diff = $diff; } else { $trend_index = $index; break; } } echo $arr_dates[$trend_index];
Output for 4.3.10 - 4.4.9, 5.0.2 - 7.2.0
100:20150616 99:20150615 96:20150612 95:20150611 94:20150610 93:20150609 92:20150608 89:20150605 88:20150604 87:20150603 86:20150602 85:20150601 13:20150529 12:20150528 5:20150521 2:20150514 9:20150507 20150507
Output for 4.3.0 - 4.3.9, 5.0.0 - 5.0.1
100:20150616 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL99:20150615 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL96:20150612 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL95:20150611 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL94:20150610 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL93:20150609 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL92:20150608 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL89:20150605 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL88:20150604 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL87:20150603 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL86:20150602 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL85:20150601 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL13:20150529 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL12:20150528 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL5:20150521 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL2:20150514 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL9:20150507 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/NGOXu on line 15 PHP_EOL20150507