DateTime PHP中的当前时间

DateTime PHP中的当前时间,php,datetime,time,Php,Datetime,Time,这是使用DateTime获取PHP中当前时间的正确方法吗 $currenttime = new DateTime(); $currenttime2 = $currenttime->format('H:i:s'); 我相信你可以这样做 $date = date('Y-m-d H:i:s'); 这将根据php.ini中的日期时间设置设置时间。创建后对$currenttime进行var_转储,以了解我的意思。您也可以使用微时间(true);获取可以使用其他函数转换的unix时间戳。Um,您尝

这是使用DateTime获取PHP中当前时间的正确方法吗

$currenttime = new DateTime();
$currenttime2 = $currenttime->format('H:i:s');

我相信你可以这样做

$date = date('Y-m-d H:i:s');

这将根据php.ini中的日期时间设置设置时间。创建后对$currenttime进行var_转储,以了解我的意思。您也可以使用微时间(true);获取可以使用其他函数转换的unix时间戳。

Um,您尝试过吗?它看起来正确吗?你可能想在它前面加上
date\u default\u timezone\u set(“美国/纽约”)或任何你的时区。除此之外应该没问题。是的,这是正确的方式,
newdatetime()但这不是日期时间,对吗?因为我需要将它与另一个DateTime()对象进行比较…如果要进行比较,则必须使用$currenttime,不要重新创建$currenttime2,因为它是一个字符串,而不是一个对象$currenttime=新的日期时间();以当前时间作为默认值创建DateTime对象。