duandi8544 2014-04-03 23:54
浏览 36
已采纳

将特定数量的WEEKDAY时间添加到字符串

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;

}
  • 写回答

1条回答 默认 最新

  • doujia6433 2014-04-04 00:42
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大