溫馨提示×

mktime php如何配合數(shù)據(jù)庫使用

PHP
小樊
85
2024-08-05 10:56:09
欄目: 編程語言

在PHP中,可以使用mktime()函數(shù)將日期和時(shí)間轉(zhuǎn)換為UNIX時(shí)間戳,然后將UNIX時(shí)間戳存儲在數(shù)據(jù)庫中。以下是一個(gè)示例代碼,演示如何使用mktime()函數(shù)和數(shù)據(jù)庫配合使用:

//連接數(shù)據(jù)庫
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

//檢查連接
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//獲取當(dāng)前日期和時(shí)間
$timestamp = mktime(0, 0, 0, date("m"), date("d"), date("Y"));

//將日期和時(shí)間存儲在數(shù)據(jù)庫中
$sql = "INSERT INTO timestamp_table (timestamp) VALUES ('$timestamp')";

if ($conn->query($sql) === TRUE) {
    echo "Record added successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

在上面的示例中,我們首先連接到數(shù)據(jù)庫,然后使用mktime()函數(shù)創(chuàng)建一個(gè)UNIX時(shí)間戳,并將其存儲在數(shù)據(jù)庫的timestamp_table表中。請根據(jù)您的實(shí)際情況修改表名和字段名。

0