PHP: dates between dates code review

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) ; ?>

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) ; ?>
_______________________________________________ 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
-- ******************************************************************************************************************** AM

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) ; ?>
_______________________________________________ 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
--
******************************************************************************************************************** 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

Some potential results on this google search? http://www.google.ca/search?hl=en&q=php+%2Bdates+enumerate&btnG=Search&meta=... On Fri, Nov 27, 2009 at 2:20 AM, Peter Karunyu <pkarunyu@gmail.com> wrote:
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) ; ?>
_______________________________________________ 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
--
******************************************************************************************************************** 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
_______________________________________________ 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

$startdate = strtotime('01-11-2009'); $enddate = strtotime('05-11-2009'); for($i=$startdate;$i<=$enddate;){ echo date("D,d M Y",$i); $i+=60*60*24; // number of seconds in 24 hrs } On Fri, Nov 27, 2009 at 10:34 AM, saidimu apale <saidimu@gmail.com> wrote:
Some potential results on this google search?
http://www.google.ca/search?hl=en&q=php+%2Bdates+enumerate&btnG=Search&meta=...
On Fri, Nov 27, 2009 at 2:20 AM, Peter Karunyu <pkarunyu@gmail.com> wrote:
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) ; ?>
_______________________________________________ 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
--
******************************************************************************************************************** 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
_______________________________________________ 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
_______________________________________________ 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

Darn! 6 lines of code is definately much better! Thanks On Fri, Nov 27, 2009 at 10:41 AM, Kiti Chigiri <kiti.chigiri@gmail.com>wrote:
$startdate = strtotime('01-11-2009'); $enddate = strtotime('05-11-2009'); for($i=$startdate;$i<=$enddate;){ echo date("D,d M Y",$i); $i+=60*60*24; // number of seconds in 24 hrs
}
On Fri, Nov 27, 2009 at 10:34 AM, saidimu apale <saidimu@gmail.com> wrote:
Some potential results on this google search?
http://www.google.ca/search?hl=en&q=php+%2Bdates+enumerate&btnG=Search&meta=...
On Fri, Nov 27, 2009 at 2:20 AM, Peter Karunyu <pkarunyu@gmail.com>wrote:
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) ; ?>
_______________________________________________ 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
--
******************************************************************************************************************** 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
_______________________________________________ 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
_______________________________________________ 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
_______________________________________________ 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

What if you are using date-pickers(javascript)?? -- Ted Turner <http://www.brainyquote.com/quotes/authors/t/ted_turner.html> - "Sports is like a war without the killing."

You mean do the same 6 lines of PHP in javascript? On Fri, Nov 27, 2009 at 11:26 AM, Isaak Mogetutu <imogetutu@googlemail.com>wrote:
What if you are using date-pickers(javascript)??
--
Ted Turner <http://www.brainyquote.com/quotes/authors/t/ted_turner.html> - "Sports is like a war without the killing." _______________________________________________ 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

If you have a date-picker functionality in your webpage and you want to get the difference in two date fields before posting to the database?? o_O? On Fri, Nov 27, 2009 at 11:33 AM, Peter Karunyu <pkarunyu@gmail.com> wrote:
You mean do the same 6 lines of PHP in javascript?
On Fri, Nov 27, 2009 at 11:26 AM, Isaak Mogetutu <imogetutu@googlemail.com
wrote:
What if you are using date-pickers(javascript)??
--
Ted Turner <http://www.brainyquote.com/quotes/authors/t/ted_turner.html> - "Sports is like a war without the killing." _______________________________________________ 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
_______________________________________________ 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
-- Stephen Leacock<http://www.brainyquote.com/quotes/authors/s/stephen_leacock.html> - "I detest life-insurance agents: they always argue that I shall some day die, which is not so."
participants (5)
-
A. Mecca | AXE360 LABS
-
Isaak Mogetutu
-
Kiti Chigiri
-
Peter Karunyu
-
saidimu apale