Home >Backend Development >PHP Tutorial >数字转换问题
一个变量值为“20150829”,如何转化成“2015年08月29日”?谢谢
<?phpheader('Content-type:text/html;charset=utf-8');$str = '20150829';function str_to_date($str){ $str1 = substr($str, 0, 4); $str2 = substr($str, 4, 2); $str3 = substr($str, 6, 2); $date = $str1 . '年' . $str2 . '月' . $str3 . '日'; return $date; }echo str_to_date($str);
$s = '20150829';echo preg_replace('/(\d{4})(\d{2})(\d{2})/', '$1年$2月$3日', $s);//或echo date('Y年m月d日', strtotime($s));