SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,002
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    have a look at PHP's 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 Code:
    <?php
    $today 
    getdate();
    print_r($today);
    ?>
    outputs:

    Code:
    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.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    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.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,002
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    maybe a switch statement would be the way to go?

    PHP Code:
    <?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(1954806400955756800);
    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 ...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •