themerlinproject
themerlinproject

Reputation: 3582

Adding specific amounts of WEEKDAY time to string

I'm looking to add a certain amount of time to a string, but skip the weekdays. So, for example if I wanted to add 40 hours to the timestamp "2014-04-03 19:33:40" it would return "2014-04-07 11:33:40" (having skipped the weekend).

I also need to do this with days and possibly minutes. Here is the (non-working) function I've come up with:

function expiration_date($opentime,$tfString)
{

    switch($tfString)
    {
        case "daily" :
            $expiration_date = gmdate('Y-m-d H:i:s', strtotime($opentime . ' + 10 weekdays'));
            break;

        case "4h":
            $expiration_date = gmdate('Y-m-d H:i:s', strtotime($opentime . ' + 40 weekday hours'));
            break;

        case "1h":
            $expiration_date = gmdate('Y-m-d H:i:s', strtotime($opentime . ' + 10 weekday hours'));
            break;

        case "30m":
            $expiration_date = gmdate('Y-m-d H:i:s', strtotime($opentime . ' + 5 weekday hours'));
            break;

        case "15m":
            $expiration_date = gmdate('Y-m-d H:i:s', strtotime($opentime . ' + 2.5 weekday hours'));
            break;

        default:        $expiration_date = '0000-00-00 00:00:00';
    }

    return $expiration_date;

}

Upvotes: 0

Views: 101

Answers (1)

John Conde
John Conde

Reputation: 219844

function addRollover($opentime , $tfString) {
    switch($tfString) {
        case "daily" :
            $interval = 'P10D'; break;
        case "4h":
            $interval = 'PT40H'; break;
        case "1h":
            $interval = 'PT10H'; break;
        case "30m":
            $interval = 'PT5H'; break;
        case "15m":
            $interval = 'PT2H30M'; break;
        default:        
            return '0000-00-00 00:00:00';
    }
    $interval = new DateInterval($interval);
    $datetime = new DateTime($opentime);
    $datetime->add($interval);

    if (in_array($datetime->format('l'), array('Sunday','Saturday'))) {
        $start = clone $datetime;
        if ('Sunday' === $datetime->format('l')) {
            $start->modify('previous Saturday');
        }
        $start->setTime(0, 0, 0);

        $diff = $start->diff($datetime); 
        $datetime->modify('next Monday'); 
        $datetime->add($diff);

        if ('daily' === $tfString) {
            $datetime->add(new DateInterval('P2D'));
        }
    }
    else if ('daily' === $tfString) {
        $datetime->add(new DateInterval('P4D'));    
    }

    return $datetime->format('Y-m-d H:i:s');
}

echo addRollOver('2014-04-03 19:33:40', '4h'); // 2014-04-07 11:33:40

See it in action

What you see happening here:

  1. We start by getting ther amount of time we need to add as a string we can pass to DateInterval(). If we can't find one we return a default time.

  2. We create our DateInterval() object.

  3. We create our DateTime() object representing our starting date and time.

  4. We add our interval to our datetime.

  5. If the resulting date and time is a weekend...

  6. We find the difference between our calculated time and midnight Saturday

  7. We then fast forward to Monday morning at Midnight

  8. We then add the difference we got on step 6

  9. If we're adding 10 days we're crossing two weekends so we need to add an extra 48 hours

Hopefully this is educational for you and introduces you to the power of PHP's DateTime capabilities.

Upvotes: 1

Related Questions