求解正確時間格式

來源:互聯網
上載者:User
$s = 'Y1320101000000XP1026629512V544930568T6B15120K20000028';$mtime = date("Ymd",mktime(0,0,0,1,1,'20'.substr($s,-14,2)) + substr($s,-12,3) * 86400)-1;echo $mtime; //得到結果是20150500


我想得到20150430的結果,怎麼修改?

註:Y1320101000000XP1026629512V544930568T6B 15120K20000028

紅色部分代表15年第120天。



回複討論(解決方案)

$s = 'Y1320101000000XP1026629512V544930568T6B15120K20000028';$mtime = strtotime('20'.substr($s,-14,2).'-00-00') + (substr($s,-12,3) * 86400);echo date('Y-m-d',$mtime);//2015-03-30

$s = 'Y1320101000000XP1026629512V544930568T6B15120K20000028';$mtime = strtotime('20'.substr($s,-14,2).'-00-00') + (substr($s,-12,3) * 86400);echo date('Y-m-d',$mtime);//2015-03-30


結果不對,不過感謝提供思路。
$s = 'Y1320101000000XP1026629512V544930568T6B15151K20000028';$mtime = strtotime('20'.substr($s,-14,2).'-01-01') + (substr($s,-12,3) * 86400)-1;echo date('Ymd',$mtime);

$s = 'Y1320101000000XP1026629512V544930568T6B15120K20000028';$mtime = date("Ymd", mktime(0, 0, 0, 1, substr($s,-12,3), substr($s,-14,2)));$d = $mtime;echo date("$d z", strtotime($d));
20150430 119

那個 '20' 可以不要,加上也沒問題

date('z') 表示年份中的第幾天(注意1月1日是第0天)
請注意表述規則上的差異

一個日期這麼隱藏。萬一資料變了豈不是很麻煩。最好能寫一個到一個function 中。

echo get_str_date('15140');function get_str_date($str){$y = substr(date('Y'),0,2);$date_str = strtotime($y.substr($str,0,2).'-01-01') + (substr($str,-3,3) * 86400)-1;return date('Y-m-d',$date_str);}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.