note 97810 added to datetime.diff

From: Date: Mon, 10 May 2010 06:24:22 +0000
Subject: note 97810 added to datetime.diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-169102@lists.php.net to get a copy of this message
For those like me who don't yet have PHP 5.3 installed on their host, here's a simple alternative to get the number of days between two dates in the format '2010-3-23' or similar acceptable to strtotime(). You need PHP 5.2. function date_diff($date1, $date2) { $current = $date1; $datetime2 = date_create($date2); $count = 0; while(date_create($current) < $datetime2){ $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current))); $count++; } return $count; } echo (date_diff('2010-3-9', '2011-4-10')." days <br \>"); ---- Server IP: 203.121.145.123 Probable Submitter: 124.121.227.49 ---- Manual Page -- http://www.php.net/manual/en/datetime.diff.php Edit -- https://master.php.net/note/edit/97810 Del: integrated -- https://master.php.net/note/delete/97810/integrated Del: useless -- https://master.php.net/note/delete/97810/useless Del: bad code -- https://master.php.net/note/delete/97810/bad+code Del: spam -- https://master.php.net/note/delete/97810/spam Del: non-english -- https://master.php.net/note/delete/97810/non-english Del: in docs -- https://master.php.net/note/delete/97810/in+docs Del: other reasons-- https://master.php.net/note/delete/97810 Reject -- https://master.php.net/note/reject/97810 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169102) next »