我有这段代码,它允许我以这种格式获取今天的日期:
$today = date("d-m-Y");
然后这个查询:
while ($row1 = mysql_fetch_array($queryenddate)) {
$end_date=$row1['end_date'];
}
结束日期打印如下:12-02-2012
我如何比较这两个日期?我需要做什么?谢谢
你可以使用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
您可以通过以下方式进行比较
if(strtotime($date1)>strtotime($date2))
您可以使用strtotime() 函数将日期转换为数字,然后可以用于比较或操作。
$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;