Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Hàm tương đương TIME_TO_SEC() của MySQL trong PHP để chuyển datetime thành giây

MySQL dùng TIME_TO_SEC(). PHP dùng strtotime() để chuyển datetime thành giây (timestamp).

MySQL:

SELECT TIME_TO_SEC(ABS(timediff('yourDateTimeValue',now())));

PHP: Cần cài XAMPP. Lưu file .php vào htdocs (ví dụ: phpproject/DifferenceInSeconds.php).

Code PHP:

<?php $MySQLDateTime='2019-01-29 13:50:40'; echo "<h1>The strtotime value is:"."</h1>".strtotime($MySQLDateTime)."<br>"; echo "<h1>The time value is:"."</h1>".time()."<br>"; $diffenceInSeconds = strtotime($MySQLDateTime)-time(); echo "<h1>The difference of both time in seconds is:"."</h1>".$diffenceInSeconds; ?>

Chạy qua trình duyệt: https://localhost:port/phpproject/ rồi click file PHP.

Kết quả hiển thị timestamp của datetime MySQL, timestamp hiện tại, và hiệu số giây.