日期格式|当年份为00个月或白天时删除00

时间:2017-06-19 09:25:31

标签: php date datetime

我想知道什么是日期绝对时删除00号码的最佳方式。我的文件中有多个日期。有没有月和日信息的日期和一些日期信息。

示例:

如果值为2016:00:00,则删除00:00`s = 2016 如果值为2016:01:10则不删除= 2016:01:10

我使用的是PHP 7

1 个答案:

答案 0 :(得分:2)

//PHP 7.0
define('DATE_START_FIELDS',["2016:00:00","2016:01:10","2017:00:00","2016:00:00","2016:02:12","2016:03:12"]);

//PHP 5.x
const DATE_END_FIELDS = ["2016:00:00","2016:01:10","2017:00:00","2016:00:00","2016:02:12","2016:03:12"];

//PHP 7.0 - function defination    
function get_dates(array $arrDate): array{
        foreach($arrDate as $date){
            list($year,$month,$day) = explode(":",$date);
            $new_dates[] = ($month != 0 && $day != 0)?$date:$year;
        }
        return $new_dates;
    }

    $start_fields = get_dates(DATE_START_FIELDS);
    $end_fields = get_dates(DATE_END_FIELDS);

    var_dump($start_fields);
    var_dump($end_fields);

根据您的评论,我创建了这个代码块!

我希望这对你有用!!!