今日写了个函数想对'月-日-年'这种日期格式进行排序。
<?php
$dates = array('10-10-2003','2-17-2002','2-16-2003','1-01-2005','10-10-2004');
function DateSort($a,$b)
{
if($a == $b)
{
retrun 0;
} list($amonth,$aday,$ayear) = explode('-',$a);
list($bmonth,$bday,$byear) = explode('-',$b); $amonth = str_pad($amonth,2,"0",STR_PAD_LEFT);
$bmonth = str_pad($bmonth,2,"0",STR_PAD_LEFT);
$aday = str_pad($aday,2,"0",STR_PAD_LEFT);
$bday = str_pad($aday,2,"0",STR_PAD_LEFT); $ayear = str_pad($ayear,2,"0",STR_PAD_LEFT);
$byear = str_pad($byear,2,"0",STR_PAD_LEFT);
$a = $ayear.$amonth.$aday;
$b = $byear.$bmonth.$bday; return ($a > $b)?1:-1;
}
usort($dates,'DateSort');
echo "<p> Sorting the array using the user-defined DateSort() function:</p>";
print_r($dates);
?>
却怎么总是出现语法错误呢?
Parse error: syntax error, unexpected T_LNUMBER in ....on line 9于是我将第九行删除了。居然可以运行了。我想问究竟我的:return 0有什么问题?小弟不才。才学十几天。求人帮忙。
<?php
$dates = array('10-10-2003','2-17-2002','2-16-2003','1-01-2005','10-10-2004');
function DateSort($a,$b)
{
if($a == $b)
{
retrun 0;
} list($amonth,$aday,$ayear) = explode('-',$a);
list($bmonth,$bday,$byear) = explode('-',$b); $amonth = str_pad($amonth,2,"0",STR_PAD_LEFT);
$bmonth = str_pad($bmonth,2,"0",STR_PAD_LEFT);
$aday = str_pad($aday,2,"0",STR_PAD_LEFT);
$bday = str_pad($aday,2,"0",STR_PAD_LEFT); $ayear = str_pad($ayear,2,"0",STR_PAD_LEFT);
$byear = str_pad($byear,2,"0",STR_PAD_LEFT);
$a = $ayear.$amonth.$aday;
$b = $byear.$bmonth.$bday; return ($a > $b)?1:-1;
}
usort($dates,'DateSort');
echo "<p> Sorting the array using the user-defined DateSort() function:</p>";
print_r($dates);
?>
却怎么总是出现语法错误呢?
Parse error: syntax error, unexpected T_LNUMBER in ....on line 9于是我将第九行删除了。居然可以运行了。我想问究竟我的:return 0有什么问题?小弟不才。才学十几天。求人帮忙。
{
retrun 0; //这里return写错了!
}
function DateSort($a,$b) {
$a = strtotime($a);
$b = strtotime($b);
if($a == $b) return 0;
return $a>$b ? 1 : -1;
}
$dates = array('10-10-2003','2-17-2002','10-10-2003','2-17-2002','2-16-2003','1-01-2005','10-10-2004');function DateFormate($date)
{
$dateAry = explode('-',$date);
return $dateAry[1]."-".$dateAry[0]."-".$dateAry[2];
}
function DateSort($a,$b)
{
$a = strtotime(DateFormate($a));
$b = strtotime(DateFormate($b));
if($a == $b) return 0;
return $a>$b ? 1 : -1;
}usort($dates,'DateSort');
echo "<p> Sorting the array using the user-defined DateSort() function:</p>";
print_r($dates);