johnsmith153 Posted December 16, 2012 Share Posted December 16, 2012 (edited) I want to pass in a timestamp of start and end dates and return an array for a dropdown. So if I pass in 1117843200 (4th June 2005) and 1186704000 (10th August 2007), it will return all 27 months June, 2005 July, 2005 August, 2005 ... etc. ... July, 2007 August, 2007 -- I also want to do the same kind of thing for full dates too. So again if I pass in the same dates as above I can choose a date from three drop downs (day / month / year). -- I tried something like this, but it's not going to work: for ($yy = $start_y; $yy <= $end_y; $yy++) { for ($mm = $start_m; $mm <= $end_m; $mm++) { $values[] = date( 'F', mktime(0, 0, 0, $mm) ) . ", " . $yy; } } Edited December 16, 2012 by johnsmith153 Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/ Share on other sites More sharing options...
requinix Posted December 16, 2012 Share Posted December 16, 2012 Try with just a single for loop, but you have to wrap the month number around yourself. for ($mm = $start_m, $yy = $start_y; $mm <= $end_m || $yy <= $end_y; $mm++) { if ($mm == 13) { $mm = 1; $yy++; } // ... } Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399652 Share on other sites More sharing options...
johnsmith153 Posted December 16, 2012 Author Share Posted December 16, 2012 Thanks Requinix, I'm looking at advancing this a bit and I've created this: It might be a bit much for a forum, so I'm considering posting it on Elance or something, but if you can point me in a direction then that would help, or maybe tell me if my instructions make sense when I post it to a job site. Thanks again for helping. -- I'm trying to create a function where I pass in a minimum and maximum timestamp and select one of 6 options: Options: (1) day (2) month (3) year (4) hour (5) minute (6) month and year The result will be an array returned with just the day, month, year, hour, minute or month and year. Month and year will be in the format "February, 2012" etc. function get_date_time($option, $min, $max) { ///etc } // returns an array of 4 - 15 echo get_date_time(1, 954806400, 955756800); // 4th April 2000 and 15th April 2000 //returns an array of 1 - 30 (because only 30 days in April) echo get_date_time(1, 954806400, 957916800); // 4th April 2000 and 10th May 2000 //returns an array of 1 - 31 (now allows 31 as 31 days in May) echo get_date_time(1, 954806400, 961459200); // 4th April 2000 and 20th June 2000 Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399656 Share on other sites More sharing options...
requinix Posted December 16, 2012 Share Posted December 16, 2012 So the function returns the widest range between those two dates? Two questions: 1. What would it return for "days" between April 10th and May 4th? 1-30? 2. What does it return for the "month and year" option? Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399659 Share on other sites More sharing options...
johnsmith153 Posted December 16, 2012 Author Share Posted December 16, 2012 Hi Requinix, It returns a list of options the user could choose from based on a min and max value. The result would be to create drop downs for date/time selections using the array as the values. From your questions: (1) Days for April 10th - May 4th = returns 1,2,3,4,10,11,12,13,14,15......29, 30 (so 1-4 and 10-30) I suppose 1-30 would be acceptable considering. (2) Option 2 month just returns the months (or a number referencing the month). So April 10th to May 4th returns April, May Option 3 year returns just the year, so April 10th to May 4th would return '2012' and March 2010 to August 2012 returns 2010, 2011, 2012 etc. Thanks for helping. Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399678 Share on other sites More sharing options...
Barand Posted December 16, 2012 Share Posted December 16, 2012 something like this, perhaps. if using option 2 or 6 do not pass times representing dates at the end of months (Jan 30 + 1 month ==> March 2) echo '<pre>'. print_r(get_date_time(6, 954806400, 966756800), true) . '</pre>'; function get_date_time($option, $start, $end) { $dt = array(); switch ($option) { case 1: $inc = 'days'; $format = 'd'; break; case 2: $inc = 'months'; $format = 'm'; break; case 3: $inc = 'years'; $format = 'Y'; break; case 4: $inc = 'hours'; $format = 'H'; break; case 5: $inc = 'minutes';$format = 'H:i'; break; case 6: $inc = 'months'; $format = 'F, Y'; break; } $i = $start; do { $dt[] = date($format, $i); $i = strtotime("+1 $inc", $i); } while ($i <= $end); return $dt; } Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399695 Share on other sites More sharing options...
johnsmith153 Posted December 16, 2012 Author Share Posted December 16, 2012 (edited) something like this, perhaps. if using option 2 or 6 do not pass times representing dates at the end of months (Jan 30 + 1 month ==> March 2) echo '<pre>'. print_r(get_date_time(6, 954806400, 966756800), true) . '</pre>'; function get_date_time($option, $start, $end) { $dt = array(); switch ($option) { case 1: $inc = 'days'; $format = 'd'; break; case 2: $inc = 'months'; $format = 'm'; break; case 3: $inc = 'years'; $format = 'Y'; break; case 4: $inc = 'hours'; $format = 'H'; break; case 5: $inc = 'minutes';$format = 'H:i'; break; case 6: $inc = 'months'; $format = 'F, Y'; break; } $i = $start; do { $dt[] = date($format, $i); $i = strtotime("+1 $inc", $i); } while ($i <= $end); return $dt; } Hi, Thanks for helping with this, but it doesn't really work. I think there's some things I could probably change though. Using this as an example: //returns an array of 1 - 30 (because only 30 days in April) echo get_date_time(1, 954806400, 957916800); // 4th April 2000 and 10th May 2000 (1) It returns 3rd April to 9th May, so it's maybe one day out either side. (2) It also returns a big long array, so in the above situation it doesn't return 30 values, it returns 37. I think a solution would be to just remove duplicate array keys, but does that seem wasteful to create a big array and then reduce it? Imagine for other scenarios where many years are used between the start/end values. Thanks again. Edited December 16, 2012 by johnsmith153 Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399698 Share on other sites More sharing options...
salathe Posted December 16, 2012 Share Posted December 16, 2012 (edited) Look into using the available date classes, such as DateTime, DateInterval and DatePeriod. They are designed to make working with dates much less painful than the archaic lets-modify-a-timestamp-in-a-loop approach of yester-year. Edited December 16, 2012 by salathe Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399700 Share on other sites More sharing options...
johnsmith153 Posted December 16, 2012 Author Share Posted December 16, 2012 I'm guessing there must be a reason why many examples I see use the old fashioned method, including Barand's example. Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399703 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2012 Share Posted December 16, 2012 I'm guessing the reason is that the old fashioned method is older. Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399704 Share on other sites More sharing options...
johnsmith153 Posted December 16, 2012 Author Share Posted December 16, 2012 Doesn't really explain Barand's answer though, unless he's just not up with the latest (which I doubt). Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399705 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2012 Share Posted December 16, 2012 It's human nature to stick with methods one knows best, and is most comfortable with. Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399716 Share on other sites More sharing options...
Barand Posted December 17, 2012 Share Posted December 17, 2012 Old dog, new tricks. "Modernised" version - although I still don't get why you would provide a range of Apr 4 to Mar 10 to produce Apr 1 to Apr 30. function get_date_time($option, $start, $end) { $dt = array(); switch ($option) { case 1: $inc = new DateInterval('P1D'); $format = 'M j'; break; case 2: $inc = new DateInterval('P1M'); $format = 'M'; break; case 3: $inc = new DateInterval('P1Y'); $format = 'Y'; break; case 4: $inc = new DateInterval('PT1H'); $format = 'H:00'; break; case 5: $inc = new DateInterval('PT1M'); $format = 'H:i'; break; case 6: $inc = new DateInterval('P1M'); $format = 'F, Y'; break; } $st = new datetime(); $st->setTimestamp($start); do { $dt[] = $st->format($format); $st->add($inc); } while ($st->getTimestamp() <= $end); return $dt; } Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399824 Share on other sites More sharing options...
Pikachu2000 Posted December 17, 2012 Share Posted December 17, 2012 Old dog, new tricks. Lol, I hope you don't think that was what I meant . . . Quote Link to comment https://forums.phpfreaks.com/topic/272052-trying-to-complete-this-date-function/#findComment-1399829 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.