原生怎么把时间转成想要的格式?

比如开始时间 2022-6-65 结束时间2022-7-25
中间一个月需要改成 29天x小时这种

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
最佳答案
function getDifference($time1, $time2)
{
    $diff = $time1 - $time2;
    $res = [];
    if ($diff >= 0) {
        $res['years'] = intval($diff / (365 * 24 * 60 * 60));
        $diff -= $res['years'] * 365 * 24 * 60 * 60;
        $res['months'] = intval($diff / (30 * 24 * 60 * 60));
        $diff -= $res['months'] * 30 * 24 * 60 * 60;
        $res['days'] = intval($diff / (24 * 60 * 60));
        $diff -= $res['days'] * 24 * 60 * 60;
        $res['hours'] = intval($diff / (60 * 60));
        $diff -= $res['hours'] * 60 * 60;
        $res['minutes'] = intval($diff / 60);
        $diff -= $res['minutes'] * 60;
        $res['seconds'] = intval($diff);
    }
    return $res;
}

var_dump(getDifference(strtotime('2022-7-25'), strtotime('2022-6-15')));
// array(6) {["years"]=> int(0)["months"]=> int(1)["days"]=> int(10)["hours"]=> int(0)["minutes"]=> int(0)["seconds"]=> int(0)}

如果你使用的是 laravel,我更建议这样

Carbon::parse('2022-7-25')->diffForHumans(Carbon::parse('2022-6-15')); // 1月前
Carbon::parse('2022-7-25')->diffForHumans(Carbon::parse('2022-6-15', true)); // 1月
1年前 评论
pndx 1年前
讨论数量: 4

转时间戳,然后写个方法自己处理

1年前 评论

可以分你个方法,不是很好,仅做参考

    /**
     * 计算两个时间相差多少天,多少小时,多少分钟
     * 
     * @param mixed $first 要比较的第一个时间 Carbon 或者时间格式
     * @param mixed $second 要比较的第二个时间 Carbon 或者时间格式
     * @param bool $format 是否格式化为字符串
     * @return string|array 
     */
    function diff_in_time($first, $second = null, $format = true)
    {
        $first = $first instanceof \Carbon\Carbon ? $first : \Carbon\Carbon::parse($first);
        $second = is_null($second) ? \Carbon\Carbon::now() : $second;
        $second = $second instanceof \Carbon\Carbon ? $second : \Carbon\Carbon::parse($second);

        $years = $first->diffInYears($second);
        $days = $first->diffInDays($second);
        $hours = $first->diffInHours($second);
        $minutes = $first->diffInMinutes($second);
        $second = $first->diffInSeconds($second);

        if (!$format) {
            return compact('years', 'days', 'hours', 'minutes', 'second');
        }

        $format_text = '';
        $start = false;
        if ($years) {
            $start = true;
            $format_text .= $years . '年';
        }
        if ($start || $days) {
            $start = true;
            $format_text .= ($days % 365) . '天';
        }

        if ($start || $hours) {
            $start = true;
            $format_text .= ($hours % 24) . '小时';
        }
        if ($start || $minutes) {
            $start = true;
            $format_text .= ($minutes % 60) . '分钟';
        }
        if ($start || $second) {
            $start = true;
            $format_text .= ($second % 60) . '秒';
        }

        return $format_text;
    }
1年前 评论
function getDifference($time1, $time2)
{
    $diff = $time1 - $time2;
    $res = [];
    if ($diff >= 0) {
        $res['years'] = intval($diff / (365 * 24 * 60 * 60));
        $diff -= $res['years'] * 365 * 24 * 60 * 60;
        $res['months'] = intval($diff / (30 * 24 * 60 * 60));
        $diff -= $res['months'] * 30 * 24 * 60 * 60;
        $res['days'] = intval($diff / (24 * 60 * 60));
        $diff -= $res['days'] * 24 * 60 * 60;
        $res['hours'] = intval($diff / (60 * 60));
        $diff -= $res['hours'] * 60 * 60;
        $res['minutes'] = intval($diff / 60);
        $diff -= $res['minutes'] * 60;
        $res['seconds'] = intval($diff);
    }
    return $res;
}

var_dump(getDifference(strtotime('2022-7-25'), strtotime('2022-6-15')));
// array(6) {["years"]=> int(0)["months"]=> int(1)["days"]=> int(10)["hours"]=> int(0)["minutes"]=> int(0)["seconds"]=> int(0)}

如果你使用的是 laravel,我更建议这样

Carbon::parse('2022-7-25')->diffForHumans(Carbon::parse('2022-6-15')); // 1月前
Carbon::parse('2022-7-25')->diffForHumans(Carbon::parse('2022-6-15', true)); // 1月
1年前 评论
pndx 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!