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
		}

	}
}

Link to comment
Share on other sites

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


Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.