我有以下几点

$var = "2010-01-21 00:00:00.0"

我想将这个日期与今天的日期进行比较(即我想知道这个$var是否在今天之前或等于今天)

我需要使用什么函数?


当前回答

试试这个:

if (date("Y-m-d",strtotime($funding_dt)) >= date("Y-m-d",strtotime('31-01-2007')))
{
    echo "ok";
} else {
    echo "not";
}

其他回答

试试这个:

if (date("Y-m-d",strtotime($funding_dt)) >= date("Y-m-d",strtotime('31-01-2007')))
{
    echo "ok";
} else {
    echo "not";
}

这种格式非常适合于标准字符串比较。

if ($date1 > $date2){
  //Action
}

要以这种格式获取今天的日期,只需使用:date("Y-m-d H:i:s")。

So:

$today = date("Y-m-d H:i:s");
$date = "2010-01-21 00:00:00";

if ($date < $today) {}

这就是这种格式的美妙之处:它很好地排序。当然,这可能会降低效率,这取决于您的具体情况,但它也可能更方便,并导致更可维护的代码-我们需要了解更多才能真正做出判断。

对于正确的时区,您可以使用,例如,

date_default_timezone_set('America/New_York');

点击这里查看可用的PHP时区。

给你:

function isToday($time) // midnight second
{
    return (strtotime($time) === strtotime('today'));
}

isToday('2010-01-22 00:00:00.0'); // true

另外,还有一些帮助函数:

function isPast($time)
{
    return (strtotime($time) < time());
}

function isFuture($time)
{
    return (strtotime($time) > time());
}

扩展Josua在w3schools上的回答:

//create objects for the dates to compare
$date1=date_create($someDate);
$date2=date_create(date("Y-m-d"));
$diff=date_diff($date1,$date2);
//now convert the $diff object to type integer
$intDiff = $diff->format("%R%a");
$intDiff = intval($intDiff);
//now compare the two dates
if ($intDiff > 0)  {echo '$date1 is in the past';}
else {echo 'date1 is today or in the future';}

我希望这能有所帮助。我在stackoverflow上的第一篇文章!

$date1=date_create("2014-07-02");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);

(w3schools的例子,它工作完美)