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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了