php中格式化某年某月某天-year、month、day

36 篇文章 0 订阅
简单记录下
/**
     * 格式化年月日[1年1月1天]
     * 2021年4月23日 09:37:04
     * author Turbo
     * @param [type] $date_text     传入字符串
     * @param [type] $year_unit     单位
     * @param [type] $month_unit    单位
     * @param [type] $day_unit      单位
     * @return void
     */
    public function formatDate($date_text, $year_unit = '年', $month_unit = '月', $day_unit = '天')
    {
        $year = 0;
        $month = 0;
        $day = 0;
        $status = true;
        $msg = '参数不正确';

        $explode_year = explode($year_unit,$date_text);
        $explode_month = explode($month_unit,$date_text);
        $explode_day = explode($day_unit,$date_text);
        $count1 = count($explode_year);
        $count2 = count($explode_month);
        $count3 = count($explode_day);

        if (($count1 == 2) && ($count2 == 2) && ($count3 == 2)) {
            // 包含有年月天
            $year = $explode_year[0];
            $month = explode($year.$year_unit,$explode_month[0])[1];
            $day = explode($year.$year_unit.$month.$month_unit,$explode_day[0])[1];
            $status = true;
            $msg = '格式化成功';
        }
        $data = ['year' => $year, 'month' => $month, 'day' => $day];
        return ['status' => $status,'data' => $data,'msg' => $msg];
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值