date_create_from_format does not give back expected result

后端 未结 2 511
时光取名叫无心
时光取名叫无心 2021-01-24 16:20

I just tried,

date_create_from_format(\'Ym\',\'201302\')

And I guess because it\'s the 29th today, it\'s actually giving me back March 1st.

2条回答
  •  不思量自难忘°
    2021-01-24 16:43

    TheWolf's solution seems to work perfectly, but here's an alternative I started writing anyway:

    function CompactStrToTime($str) {
        $year = strlen($str)>=4 ? substr($str,0,4) : date('Y');
        $month = strlen($str)>=6 ? substr($str,4,2) : 1;
        $day = strlen($str)>=8 ? substr($str,6,2) : 1;
        $hour = strlen($str)>=10 ? substr($str,8,2) : 0;
        $min = strlen($str)>=12 ? substr($str,10,2) : 0;
        $sec = strlen($str)>=14 ? substr($str,12,2) : 0;
        return mktime($hour,$min,$sec,$month,$day,$year);
    }
    

提交回复
热议问题