发布两个日期函数,用于日期操作

浏览:973 发布日期:2015/05/29 分类:技术分享 关键字: 日期函数
开始做项目了,才发现很多东西都说不准。很多细节都需要注意。这就遇到一些小问题,时间对象的运算。从网上找了一些资料,摘抄了一位兄台写的。以作备忘。    function DateDiff($part, $begin, $end)
    {
        //strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。
        $diff = strtotime($end) - strtotime($begin);
        switch($part)
        {
            //将二个高精确度数字相除
            case "y": $retval = bcdiv($diff, (60 * 60 * 24 * 365)); break;
            case "m": $retval = bcdiv($diff, (60 * 60 * 24 * 30)); break;
            case "w": $retval = bcdiv($diff, (60 * 60 * 24 * 7)); break;
            case "d": $retval = bcdiv($diff, (60 * 60 * 24)); break;
            case "h": $retval = bcdiv($diff, (60 * 60)); break;
            case "n": $retval = bcdiv($diff, 60); break;
            case "s": $retval = $diff; break;
        }
        return $retval;
    }
    Function DateAdd($part, $n, $date)
    {
        switch($part)
        {
            case "y": $val = date("Y-m-d H:i:s", strtotime($date ." +$n year")); break;
            case "m": $val = date("Y-m-d H:i:s", strtotime($date ." +$n month")); break;
            case "w": $val = date("Y-m-d H:i:s", strtotime($date ." +$n week")); break;
            case "d": $val = date("Y-m-d H:i:s", strtotime($date ." +$n day")); break;
            case "h": $val = date("Y-m-d H:i:s", strtotime($date ." +$n hour")); break;
            case "n": $val = date("Y-m-d H:i:s", strtotime($date ." +$n minute")); break;
            case "s": $val = date("Y-m-d H:i:s", strtotime($date ." +$n second")); break;
        }
        return $val;
    }
最佳答案
评论( 相关
后面还有条评论,点击查看>>