我有这个代码,允许我以这种格式抓住今天的日期:
$today = date("d-m-Y");
然后这个查询:
while ($row1 = mysql_fetch_array($queryenddate)) {
$end_date=$row1['end_date'];
}
结束日期打印类似于:12-02-2012
我如何比较这两个日期? 我需要做什么? 感谢
答案 0 :(得分:5)
您可以使用DateTime
$today = new DateTime();
$date = DateTime::createFromFormat("d-m-Y", $end_date);
if ($today > $date) {
// Totaay is more current
}
if ($today->format("m") == $date->format("m")) {
// they have same month
}
您也可以这样做
$diffrence = $today->diff($date); //enddate is "01-01-2010"
var_dump($diffrence);
输出
object(DateInterval)[3]
public 'y' => int 2
public 'm' => int 8
public 'd' => int 24
public 'h' => int 0
public 'i' => int 0
public 's' => int 0
public 'invert' => int 1
public 'days' => int 998
答案 1 :(得分:4)
您可以通过
进行比较if(strtotime($date1)>strtotime($date2))
答案 2 :(得分:1)
您可以使用strtotime()函数将日期转换为数字,然后您可以用于比较或opereaciones。
$today = date("d-m-Y");
$end_date='02-09-2012';
echo $today;
echo "<br>".$end_date;
$diference= (strtotime($end_date)-strtotime($today))/86400;
$diference= abs($diference);
$diference= floor($diference);
echo "<br>".$diference;