Date function generation

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

Hi there,

have a look at PHP’s [getdate()](http://docs.php.net/getdate) function, which returns an associative array containing the date information of the timestamp given (if no timestamp is provided it defaults to the current local time).

E.g.

<?php
$today = getdate();
print_r($today);
?>

outputs:


Array ( [seconds] => 40
          [minutes] => 3
          [hours] => 15
          [mday] => 16
          [wday] => 0
          [mon] => 12
          [year] => 2012
          [yday] => 350
          [weekday] => Sunday
          [month] => December
          [0] => 1355666620 )

Although this doesn’t do exactly what you want, it should be a good place to get started.

Thanks, this helps.

I need a little help with the calculation side of things though.

E.g. I’ve been trying to do things like this for option 6:


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;

}

}

…but i’s pretty rubbish, and clearly isn’t going to work.

Hi,

maybe a switch statement would be the way to go?

<?php function get_date_time($i, $t1, $t2){
  
  $t1 = getdate($t1);
  $t2 = getdate($t2);
  
  switch ($i) {
    case 1:
      return ...;
    case 2:
      return ...;
    case 3:
      return ...;
  }
}

echo get_date_time(1, 954806400, 955756800);

However, I’m not too sure what you’re trying to return from each option.
If this doesn’t help, give me an example with the same two timestamps, of what the output should be for each option and I’ll see what I can do.

e.g.
echo get_date_time(1, 954806400, 955756800); should output …
echo get_date_time(2, 954806400, 955756800); should output …
echo get_date_time(3, 954806400, 955756800); should output …