Jump to content

[SOLVED] Listing dates between date1 and date2


Jahren

Recommended Posts

Hi!

I'm a php coding beginner onward to intermediate level :P

 

I want to list all days between date1 and date2

ie:

Jan 29 2009 - Jan 30 2009 - Jan 31 2009 - Feb 1 2009 - Feb 2 2009 - etc..

 

I can fetch my begin_date and end_date from MySQL fine but I have no clue on how to generate every day between them

 

I want to add the days in a html select box for the user to pick one from...

 

I though about this logic but obviously won't work at all.

//Classe de gestion de dates, opérations arithmétiques sur des dates
class dates_operations
{
	function test()
	{
		$begindate = date("M-d-Y", mktime (0,0,0,1,25,2009));
		$enddate= date("M-d-Y", mktime mktime (0,0,0,2,6,2009));

		$date = $begindate;
		echo $date;

		while ($date < $enddate)
		{
			$date = $date("j M Y", strtotime("+1 day"));
			echo $date
		}

	}
}

Ok, I have a function that I have used for dates but I haven't used it for dates coming from a database. But here is the code if you want to play about with your own date formatting  :)

 


<?php
$startDate = '02/27/2007';
$endDate = '03/28/2007';
    $days = (strtotime($endDate) - strtotime($startDate)) / 86400 + 1;
    $startMonth = date("m", strtotime($startDate));
    $startDay = date("d", strtotime($startDate));
    $startYear = date("Y", strtotime($startDate));    
    $dates= array();
    for($i=0; $i<$days; $i++){
        $dates[$i] = date("d/m/Y", mktime(0, 0, 0, $startMonth , ($startDay+$i), $startYear)); 
} 
	print_r($dates); 
?>


strtotime() returns a UNIX timestamp, which is a measure of time in seconds since 1970.  therefore if you take the difference of the two dates, you get the number of second between them.  there are 60*60*24 seconds in a day (60s in a min, 60min in an hr, 24hrs in a day), or 86400.  essentially dividing by 86400 gives you the number of days between the dates (to which you must add 1 to land at the end date).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.