Home  >  Article  >  Backend Development  >  求解正确时间格式

求解正确时间格式

WBOY
WBOYOriginal
2016-06-20 12:56:101460browse

$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);}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn