我有以下几点

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

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

我需要使用什么函数?


当前回答

扩展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上的第一篇文章!

其他回答

试试这个:

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

如果你做事有时间和日期,碳是你最好的朋友;

安装包,然后:

$theDay = Carbon::make("2010-01-21 00:00:00.0");

$theDay->isToday();
$theDay->isPast();
$theDay->isFuture();
if($theDay->lt(Carbon::today()) || $theDay->gt(Carbon::today()))

Lt =小于, Gt =大于

在这个问题中:

$theDay->gt(Carbon::today()) ? true : false;

还有更多;

你可以使用DateTime类:

$past   = new DateTime("2010-01-01 00:00:00");
$now    = new DateTime();
$future = new DateTime("2021-01-01 00:00:00");

比较运算符工作*:

var_dump($past   < $now);         // bool(true)
var_dump($future < $now);         // bool(false)

var_dump($now == $past);          // bool(false)
var_dump($now == new DateTime()); // bool(true)
var_dump($now == $future);        // bool(false)

var_dump($past   > $now);         // bool(false)
var_dump($future > $now);         // bool(true)

也可以从DateTime对象中获取时间戳值并进行比较:

var_dump($past  ->getTimestamp());                        // int(1262286000)
var_dump($now   ->getTimestamp());                        // int(1431686228)
var_dump($future->getTimestamp());                        // int(1577818800)
var_dump($past  ->getTimestamp() < $now->getTimestamp()); // bool(true)
var_dump($future->getTimestamp() > $now->getTimestamp()); // bool(true)

*注意===在比较两个不同的DateTime对象时返回false,即使它们表示相同的日期。

strtotime($var);

将其转换为时间值

time() - strtotime($var);

给出自$var后的秒数

if((time()-(60*60*24)) < strtotime($var))

将检查$var是否在最后一天内。

给你:

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());
}