TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Getting the dates between 2 dates (http://www.talkphp.com/general/4039-getting-dates-between-2-dates.html)

kawasaki 03-13-2009 07:11 PM

Getting the dates between 2 dates
 
I need to figure out a way to get the dates between $startdate and $enddate. Then insert all of them in to a MySQL table. This is for a small events calendar i've developed.

I've come across this function, just not sure how to grab each individual date and insert it into the table.
This is a simple snippet of code that will return an array of days between two dates.

Code
PHP Code:

<?php
function GetDays($sStartDate$sEndDate){
  
// Firstly, format the provided dates.
  // This function works best with YYYY-MM-DD
  // but other date formats will work thanks
  // to strtotime().
  
$sStartDate gmdate("Y-m-d"strtotime($sStartDate));
  
$sEndDate gmdate("Y-m-d"strtotime($sEndDate));
 
  
// Start the variable off with the start date
  
$aDays[] = $sStartDate;
 
  
// Set a 'temp' variable, sCurrentDate, with
  // the start date - before beginning the loop
  
$sCurrentDate $sStartDate;
 
  
// While the current date is less than the end date
  
while($sCurrentDate $sEndDate){
    
// Add a day to the current date
    
$sCurrentDate gmdate("Y-m-d"strtotime("+1 day"strtotime($sCurrentDate)));
 
    
// Add this new day to the aDays array
    
$aDays[] = $sCurrentDate;
  }
 
  
// Once the loop has finished, return the
  // array of days.
  
return $aDays;
}
?>

Any Ideas? This is for inserting entries into a calendar FROMdate TOdate. Know what i mean? Thank you for your suggestions.

Kawasaki@Nexuz

Tanax 03-13-2009 07:17 PM

Please use [php] or [highlight=php] tags around the phpcode. It will be easier for us to read your code then - thus making it easier for us to help you.

kawasaki 03-14-2009 03:35 PM

k, any ideas? What i want to accomplish is to be able to give a start date and end date and have it insert all of the dates from start to finish into the event table. Also Maybe if i had a startDate and then span # of days.

Village Idiot 03-14-2009 03:48 PM

Assuming that dateA and dateB are unix timestamps representing dates where dateA < dateB, this should work:

PHP Code:

//1 day = seconds/hours in day/minutes in hours/seconds in minute)
//Floor because it will be the lowest integer
$startDay floor($dateA/24/60/60);
$endDay =  floor($dateB/24/60/60);
$daysDiff $endDay-$startDay
for($i=0;i<$daysDiff;$i++)
{
//This is the start date plus $i days.
$day Date("y-m-d",$startDay*24*60*60+$i*24*60*60)


Basically what this does is:
Convert timestamp A into days
Convert timestamp B into days
Get the difference
Starting from the first day, represent that day plus 0 to the difference.

It would, however, be more efficient to store the start date and the end date in the database and have the script (or the query) generate your dates opposed to inserting each one.

Salathe 03-14-2009 04:16 PM

You haven't provided much information on the database structure, but wouldn't it be more efficient to store the start and end date/time on a per-event basis and deal with which days an individual item covers in the frontend code? Unless there's a reason each day must be saved individually?..


All times are GMT. The time now is 11:22 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0