我如何比较php中的两个日期? 我有以下日期。一个来自DB。另一个来自Datepicker
$dbdate = 01-06-201402-06-201403-04-201405-06-2014 //DB date
$datepickerDate = 06-06-2014 //Datepicker date.
此处$dbdate
位于foreach循环中,两种格式均为dd-mm-yyyy。如何比较datepicker中的单个日期与$dbdate
中的日期?
答案 0 :(得分:0)
与for {循环中的STRTOTIME函数比较
return STRTOTIME($dbdate) === STRTOTIME($datepickerDate);
答案 1 :(得分:0)
最好的事情是将所有转换为unix时间戳..
php方法是strtotime()
然后它返回整数值...你可以比较这些值
http://www.php.net//manual/en/function.strtotime.php
但事情是你的dbdate一次解析超过1个日期..我应该一次获得1个日期
答案 2 :(得分:0)
这不是一个理想的解决方案 - 但我希望它可以帮助你; - )。
<?php
// Input
$dbdate = '01-06-201402-06-201403-04-201405-06-2014';
$datepickerDate = '06-06-2014';
// Please note: it would be possible to check dates directly in this for loop.
// For educational purposes I splitted it.
$datepickerDateTime = strtotime($datepickerDate);
// Transform concatenated dates to separate dates in array.
$datesList = array();
$dateOffset = 10;
for($iCurrentDateOffset = 0; $iCurrentDateOffset < strlen($dbdate); $iCurrentDateOffset += $dateOffset) {
$datesList[] = substr($dbdate, $iCurrentDateOffset, $dateOffset);
}
// Compare each separate date to $datepickerDate
foreach($datesList as $date) {
if(strtotime($date) < $datepickerDateTime) {
echo("$date is before $datepickerDate<br />");
} else if(strtotime($date) == $datepickerDateTime) {
echo("$date is equal to $datepickerDate<br />");
} else if(strtotime($date) > $datepickerDateTime) {
echo("$date is past $datepickerDate<br />");
}
}
// ...
输出是: