是否可以使用包含日期的变量设置新日期时间的日期


Is it possible to set the date of new DateTime with a variable containing a date?

我正在尝试创建一个if statement,用于检查当前日期是否大于用户设置并保存在我的DB中的日期。我已经测试了一个固定日期,它可以工作:

if (new DateTime() > new DateTime("2016-02-25 09:00:00")) {
                        echo "test";
                    }

但是,这会起作用吗?

$PlannedStartDate = $ProjectRow['StartDate'];
if (new DateTime() > new DateTime("$PlannedStartDate 09:00:00")) {
                        echo "test";
                        }

$PlannedStartDate = 2016-01-19

DateTime 构造需要两个参数,其中第二个参数必须是 DateTimeZone。

因此,您的代码由于拼写错误而失败。这是一个工作:

$PlannedStartDate = "16:00:00";
if (new DateTime() > new DateTime($PlannedStartDate)) {
    echo "test";
}