How to fix dates to be similar format?

前端 未结 2 933
滥情空心
滥情空心 2021-01-24 11:04

I am receiving 2 date formats:

1/22/2020

1/22/20

I need to convert 1/22/2020 to 1/22/20

I currently use the

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-24 11:18

    Do you need this:

    $str1 = "1/22/2024";
    $str2 = "1/22/24";
    
    
    
    function get_needed_format($str) {
        if(strlen(explode("/", $str)[2]) === 4) {
            return substr($str, 0, -4).substr($str, -2);
        }
    
        return $str;
    }
    
    
    echo get_needed_format($str1);
    echo "\n";
    echo get_needed_format($str2);
    

    ?

提交回复
热议问题