note 81753 added to function.strtotime
| From: | timhavens at gmail dot com | Date: | Wed, 12 Mar 2008 13:43:22 +0000 |
| Subject: | note 81753 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136969@lists.php.net to get a copy of this message | ||
I needed a function to return all the dates between two dates. I've looked for something like
this in the past an didn't find it. I'm sure there are better ways, but I wanted to post
this in case it was helpful to others.
function dates_between_dates($date1,$date2) {
$return_array = array();
$num_days = date_diff('d',$date1,$date2)+1;
for($i=0;$i<=$num_days;$i++) {
$return_array[] = date('Y-m-d',strtotime($date1."+$i day"));
}
return $return_array;
}
function date_diff($interval, $date1, $date2)
{
//convert the dates into timestamps
$date1 = strtotime($date1);
$date2 = strtotime($date2);
$seconds = $date2 - $date1;
if ($seconds < 0)
{
$tmp = $date1;
$date1 = $date2;
$date2 = $tmp;
$seconds = 0-$seconds;
}
//reconvert the timestamps into dates
if ($interval =='y' || $interval=='m') {
$date1 = date("Y-m-d h:i:s", $date1);
$date2= date("Y-m-d h:i:s", $date2);
}
switch($interval) {
case "y":
list($year1, $month1, $day1) = split('-', $date1);
list($year2, $month2, $day2) = split('-', $date2);
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) +
(date('s',$date1)
);
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) +
(date('s',$date2)
);
$diff = $year2 - $year1;
if($month1 > $month2) {
$diff -= 1;
} elseif($month1 == $month2) {
if($day1 > $day2) {
$diff -= 1;
} elseif($day1 == $day2) {
if($time1 > $time2) {
$diff -= 1;
}
}
}
break;
case "m":
list($year1, $month1, $day1) = split('-', $date1);
list($year2, $month2, $day2) = split('-',$date2);
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) +
(date('s',$date1)
);
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) +
(date('s',$date2)
);
$diff = ($year2 * 12 + $month2) - ($year1 * 12 + $month1);
if($day1 > $day2) {
$diff -= 1;
} elseif($day1 == $day2) {
if($time1 > $time2) {
$diff -= 1;
}
}
break;
case "w":
// Only simple seconds calculation needed from here on
$diff = floor($seconds / 604800);
break;
case "d":
$diff = floor($seconds / 86400);
break;
case "h":
$diff = floor($seconds / 3600);
break;
case "i":
$diff = floor($seconds / 60);
break;
case "s":
$diff = $seconds;
break;
}
return $diff;
}
----
Server IP: 209.41.74.194
Probable Submitter: 70.230.151.196
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- https://master.php.net/note/edit/81753
Del: integrated -- https://master.php.net/note/delete/81753/integrated
Del: useless -- https://master.php.net/note/delete/81753/useless
Del: bad code -- https://master.php.net/note/delete/81753/bad+code
Del: spam -- https://master.php.net/note/delete/81753/spam
Del: non-english -- https://master.php.net/note/delete/81753/non-english
Del: in docs -- https://master.php.net/note/delete/81753/in+docs
Del: other reasons-- https://master.php.net/note/delete/81753
Reject -- https://master.php.net/note/reject/81753
Search -- https://master.php.net/manage/user-notes.php