我有这样的php代码,我的输出正确吗?日期格式为 (年/月/日)
年月和日期格式>> 120924(就像2012/09/24)
$yearchk=(int)substr($date,0,4);
$monthchk=(int)substr($date,5,2);
$daychk=(int)substr($date,8,2);
在这里,有一个 Substring.Is 此输出在子字符串中是正确的。
My Yearchk output > 12
my monthck output > 09
my daychk output > 24
此输出是否正确?
不要使用 substr。
使用日期和时间函数来执行此操作。
这样做:-
<?
$userdate = "2012/09/24";
$y = date('Y',strtotime($userdate));
$m = date('m',strtotime($userdate));
$d = date('d',strtotime($userdate));
echo 'Year: ' . $y . ' Month: ' . $m . ' Date: ' . $d;
参考现场演示
不,你没有substr()
正确的论据。这将起作用:
$date = '120924';
$yearchk=(int)substr($date,0,2);
$monthchk=(int)substr($date,2,2);
$daychk=(int)substr($date,4,2);
// $yearchk -> 12
// $monthchk -> 9
// $daychk -> 24
请记住,当您将它们转换为(int)
时,您将丢失任何前导零。