Jahren Posted November 27, 2008 Share Posted November 27, 2008 Hi! I'm a php coding beginner onward to intermediate level 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 } } } Quote Link to comment Share on other sites More sharing options...
waterssaz Posted November 27, 2008 Share Posted November 27, 2008 What format are the dates in from your database and I should be able to help you? Quote Link to comment Share on other sites More sharing options...
Jahren Posted November 27, 2008 Author Share Posted November 27, 2008 I use regular mySQL date format ie : 2009-02-04 thanks Quote Link to comment Share on other sites More sharing options...
waterssaz Posted November 27, 2008 Share Posted November 27, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
Jahren Posted November 27, 2008 Author Share Posted November 27, 2008 oh wow, Nice way of doing it i'll find an easy way to convert it to mysql dates im pretty sure. thanks a bundle it works fine in test. I'll try and fit this function in my design and give you feedback about it. thanks again Quote Link to comment Share on other sites More sharing options...
Jahren Posted November 27, 2008 Author Share Posted November 27, 2008 Can I ask something about the code so I understand it all ? what does '86400 + 1;' do? Quote Link to comment Share on other sites More sharing options...
akitchin Posted November 27, 2008 Share Posted November 27, 2008 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). Quote Link to comment Share on other sites More sharing options...
Jahren Posted November 27, 2008 Author Share Posted November 27, 2008 Ahhh, that makes more sense now thanks for the reply btw i'm still implementing the solution to my project. Hold on for feedback Quote Link to comment Share on other sites More sharing options...
Jahren Posted November 27, 2008 Author Share Posted November 27, 2008 so mktime(0, 0, 0, $startMonth , ($startDay+$i), $startYear)) is able to switch month on his own if we get to day 32 for exemple? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.