Jump to content

Trying To Complete This Date Function


johnsmith153

Recommended Posts

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 by johnsmith153
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}  

Link to comment
Share on other sites

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 by johnsmith153
Link to comment
Share on other sites

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;
} 

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.