문제

When I try to output a DateTime object, it is literally printing the format, e.g.

Y-m-d H:i:s

Code

$db = new PDO('mysql:host=localhost;dbname=xxx;charset=utf8', 'xxx', 'xxx');
$dbTime = new DateTime(current($db->query('SELECT NOW()')->fetchAll(PDO::FETCH_COLUMN, 0)));
$myTime = new DateTime();
$diff = $myTime->diff($dbTime);
echo $diff->format('Y-m-d H:i:s');

From reading other questions and examples this should work so I'm confused why it's not.

도움이 되었습니까?

해결책

The DateTime::diff method returns a DateInterval object. Your getting unexpected results because you are calling format on a DateInterval object which is different to the implementation on the DateTime object. see http://php.net/manual/en/dateinterval.format.php for more info on how to use it.

다른 팁

The output from DateTime::diff() is a DateInterval object, which has rather different formatting Meanings.... in particular read about the overflow. Note also the use of % symbols to force format characters, otherwise they're treated as string characters

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top