note 97810 modified in datetime.diff by danbrown

From: Date: Mon, 10 May 2010 12:13:31 +0000
Subject: note 97810 modified in datetime.diff by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169119@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. <?php 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 \>"); ?> --was-- 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 \>"); http://php.net/manual/en/datetime.diff.php

« previous php.notes (#169119) next »