How to round unix timestamp up and down to nearest half hour?

后端 未结 10 1933
名媛妹妹
名媛妹妹 2020-12-14 17:16

Ok so I am working on a calendar application within my CRM system and I need to find the upper and lower bounds of the half an hour surrorunding the timestamp at which someb

相关标签:
10条回答
  • 2020-12-14 17:30

    As you probably know, a UNIX timestamp is a number of seconds, so substract/add 1800 (number of seconds in 30 minutes) and you will get the desired result.

    0 讨论(0)
  • 2020-12-14 17:31

    If you need to get the current time and then apply the rounding (down) of the time, I would do the following:

    $now = date('U');
    $offset = ($now % 1800);
    $now = $now-$offset;
    for ($i = 0;$i < 24; $i++)
    {
        echo date('g:i',$now);
        $now += 1800;
    }
    

    Or you could round up by adding the offset, and do something more than just echo the time. The for loop then displays the 12 hours of increments. I used the above in a recent project.

    0 讨论(0)
  • 2020-12-14 17:32

    You could use the modulo operator.

    $time -= $time % 3600; // nearest hour (always rounds down)
    

    Hopefully this is enough to point you in the right direction, if not please add a comment and I'll try to craft a more specific example.

    0 讨论(0)
  • 2020-12-14 17:32

    This is a solution using DateTimeInterface and keeping timezone information etc. Will also handle timezones that are not a multiple of 30 minutes offset from GMT (e.g. Asia/Kathmandu).

    /**
     * Return a DateTimeInterface object that is rounded down to the nearest half hour.
     * @param \DateTimeInterface $dateTime
     * @return \DateTimeInterface
     * @throws \UnexpectedValueException if the $dateTime object is an unknown type
     */
    function roundToHalfHour(\DateTimeInterface $dateTime)
    {
        $hours = (int)$dateTime->format('H');
        $minutes = $dateTime->format('i');
    
        # Round down to the last half hour period
        $minutes = $minutes >= 30 ? 30 : 0;
    
        if ($dateTime instanceof \DateTimeImmutable) {
            return $dateTime->setTime($hours, $minutes);
        } elseif ($dateTime instanceof \DateTime) {
            // Don't change the object that was passed in, but return a new object
            $dateTime = clone $dateTime;
            $dateTime->setTime($hours, $minutes);
            return $dateTime;
        }
        throw new UnexpectedValueException('Unexpected DateTimeInterface object');
    }
    

    You'll need to have created the DateTime object first though - perhaps with something like $dateTime = new DateTimeImmutable('@' . $timestamp). You can also set the timezone in the constructor.

    0 讨论(0)
  • 2020-12-14 17:43

    PHP does have a DateTime class and a whole slough of methods that it provides. You could use these if you like, but I find it easier to use the built-in date() and strtotime() functions.

    Here's my solution:

    // Assume $timestamp has the original timestamp, i.e. 2012-03-09 16:23:41
    
    $day = date( 'Y-m-d', $timestamp ); // $day is now "2012-03-09"
    $hour = (int)date( 'H', $timestamp ); // $hour is now (int)16
    $minute = (int)date( 'i', $timestamp ); // $minute is now (int)23
    
    if( $minute < 30 ){
      $windowStart = strtotime( "$day $hour:00:00" );
      $windowEnd   = strtotime( "$day $hour:30:00" );
    } else {
      $windowStart = strtotime( "$day $hour:30:00" );
      if( ++$hour > 23 ){
        // if we crossed midnight, fix the date and set the hour to 00
        $day = date( 'Y-m-d', $timestamp + (24*60*60) );
        $hour = '00';
      }
      $windowEnd   = strtotime( "$day $hour:00:00" );
    }
    
    // Now $windowStart and $windowEnd are the unix timestamps of your endpoints
    

    There are a few improvements that can be made on this, but that's the basic core.

    [Edit: corrected my variable names!]

    [Edit: I've revisited this answer because, to my embarrassment, I realized that it didn't handle the last half-hour of a day correctly. I've fixed that issue. Note that $day is fixed by adding a day's worth of seconds to the timestamp -- doing it this way means we don't have to worry about crossing month boundaries, leap days, etc. because PHP will format it correctly for us regardless.]

    0 讨论(0)
  • 2020-12-14 17:46

    I didn't read the questions clearly, but this code will round to the nearest half hour, for those who don't need the range between the two. Uses some of SenorAmor's code. Props and his mad elegant solution to the correct question.

    $time = 1330518155; //Or whatever your time is in unix timestamp
    
    //Store how many seconds long our rounding interval is
    //1800 equals one half hour
    //Change this to whatever interval to round by
    $INTERVAL_SECONDS = 1800;  //30*60
    
    //Find how far off the prior interval we are
    $offset = ($time % $INTERVAL_SECONDS); 
    
    //Removing this offset takes us to the "round down" half hour
    $rounded = $time - $offset; 
    
    //Now add the full interval if we should have rounded up
    if($offset > ($INTERVAL_SECONDS/2)){
      $nearestInterval = $rounded + $INTERVAL_SECONDS;
    }
    else{
      $nearestInterval = $rounded 
    }
    
    0 讨论(0)
提交回复
热议问题