Nah, I am not coding my own date function.

Let me give an example; given the date 01-11-2009 and 05-11-2009, I want the said function to return the following:
01-11-2009, 02-11-2009, 03-11-2009, 04-11-2009 and 05-11-2009

i.e. dates between dates :-)

On Fri, Nov 27, 2009 at 10:13 AM, A. Mecca | AXE360 LABS <axe360@gmail.com> wrote:
Dates are always interesting in any application. Am confused, y are u coding your own date function.
I think there is a function that is inbuilt to do that so you might want to look at PHP date funtions if you havent already.

On Wed, Nov 25, 2009 at 1:53 PM, Peter Karunyu <pkarunyu@gmail.com> wrote:
I have been pulling out my hair for the last few hours...

Given two dates in whichever format, e.g. 2009-10-06 and November 12, 2009, which is the best logical approach here to get all dates in between those two dates?

I was thinking of converting the received dates into timestamps, then using mktime to add one day to the older timestamp till i get to the new one, but a small voice tells me there is a better way. Any ideas?

Here is the code I am using.

<?php

/**
 * Get the days between two dates and return those actual dates in an array
*/
function getDaysBetweenDates($startDate, $endDate){
    //initialize array that will get returned
    $daysBetweenDatesArray = array();
   
    //attempt to convert the received dates into timestamps
    $startDateTimestamp = strtotime($startDate);
    $endDateTimestamp = strtotime($endDate);
   
    /*TODO: Problem 1: even if the received dates are bogus, strtotime() will convert them into
    sane timestamps, which might pass the checkdate() validation below
    */
   
    //check that the received values are valid
    if (!checkdate(date('n',$startDateTimestamp), date('j',$startDateTimestamp), date('Y',$startDateTimestamp))) {
        //do some error handling here
        return false;
    }
   
    if (!checkdate(date('n',$endDateTimestamp), date('j',$endDateTimestamp), date('Y',$endDateTimestamp))) {
        //do some error handling here
        return false;
    }
   
    //dates received are probably valid
   
   
    /*
    Logic 1: One day has (24*60*60) seconds, therefore, if I can get the number of 86400 seconds between those two days,
    I can get those actual dates. Havent used this method
   
    Logic 2: Use mktime to add One to the start date, then increment $startDateTimestamp with 86400, i.e. the number of seconds in one day
    and loop it untill $startDateTimestamp is less than or equal to $endDateTimestamp.
    */
   
    //variable to count the number of times the loop has run, will be used as index in the $daysBetweenDatesArray array
    $loopCounter = 1;
   
    //variable to store the dates found in between, not really necessary, the dates found could be put directly in the array
    $dateInBetweenTimestamp = '';
   
    //assume startDate is included, put it as first element in array
    $daysBetweenDatesArray[0] = date('j-n-Y', $startDateTimestamp);
   
    while ($startDateTimestamp <= $endDateTimestamp) {
        //in order to use mktime, we will need seconds, minutes, hours, day, month and year for each date
        $startDateSecond = date('s',$startDateTimestamp);
        $startDateMinute = date('i',$startDateTimestamp);
        $startDateHour = date('G',$startDateTimestamp);
        $startDateDay = date('j',$startDateTimestamp);
        $startDateMonth = date('n',$startDateTimestamp);
        $startDateYear = date('Y',$startDateTimestamp);
       
        $dateInBetweenTimestamp = mktime($startDateHour, $startDateMinute, $startDateSecond, $startDateMonth, $startDateDay+1, $startDateYear);
        $daysBetweenDatesArray[$loopCounter] = date('j-n-Y', $dateInBetweenTimestamp);
       
        //increment loop counter
        $loopCounter++;
       
        //increment $startDateTimestamp
        $startDateTimestamp = $startDateTimestamp + 86400;
    }
   
    return $daysBetweenDatesArray;
}

?>

How to use:
<?php
$datesInBetween = getDaysBetweenDates('2009-01-01', '2009-01-30');
print_r($datesInBetween)    ;   
?>




--
********************************************************************************************************************
AM



_______________________________________________
Skunkworks mailing list
Skunkworks@lists.my.co.ke
http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks
------------
Skunkworks Rules
http://my.co.ke/phpbb/viewtopic.php?f=24&t=94
------------
Other services @ http://my.co.ke
Other lists
-------------
Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce
Science:  http://lists.my.co.ke/cgi-bin/mailman/listinfo/science
kazi:     http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general