note 104146 added to function.date-diff

From: Date: Thu, 26 May 2011 06:59:31 +0000
Subject: note 104146 added to function.date-diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-180051@lists.php.net to get a copy of this message
/** * Get the difference between the dates without days off */ function get_date_diff($date1, $date2) { $holidays = 0; for ($day = $date2; $day < $date1; $day += 24 * 3600) { $day_of_week = date('N', $day); if($day_of_week > 5) { $holidays++; } } return $date1 - $date2 - $holidays * 24 * 3600; } function test_get_date_diff() { $datas = array( array('Fri 20 May 2011 14:00:00', 'Fri 20 May 2011 13:00:00', 1 * 3600), array('Sat 21 May 2011 15:00:00', 'Fri 20 May 2011 13:00:00', 2 * 3600), array('Sun 22 May 2011 16:00:00', 'Fri 20 May 2011 13:00:00', 3 * 3600), array('Mon 23 May 2011 14:00:00', 'Fri 20 May 2011 13:00:00', 25 * 3600), array('Fri 27 May 2011 13:00:00', 'Fri 13 May 2011 13:00:00', 24 * 10 * 3600), ); foreach ($datas as &$data) { $actual = get_date_diff(strtotime($data[0]), strtotime($data[1])); if ($actual != $data[2]) { echo "Test for get_date_diff faled expected {$data[2]} but was {$actual}, date1: {$data[0]}, date2: {$data[1]}.<br>"; } } } test_get_date_diff($data); ---- Server IP: 69.147.83.197 Probable Submitter: 213.145.151.30 ---- Manual Page -- http://www.php.net/manual/en/function.date-diff.php Edit -- https://master.php.net/note/edit/104146 Del: integrated -- https://master.php.net/note/delete/104146/integrated Del: useless -- https://master.php.net/note/delete/104146/useless Del: bad code -- https://master.php.net/note/delete/104146/bad+code Del: spam -- https://master.php.net/note/delete/104146/spam Del: non-english -- https://master.php.net/note/delete/104146/non-english Del: in docs -- https://master.php.net/note/delete/104146/in+docs Del: other reasons-- https://master.php.net/note/delete/104146 Reject -- https://master.php.net/note/reject/104146 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#180051) next »