如何在php和mysql中使用date?

2jcobegt  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(276)

我想知道如何在mysql和php中处理日期。我想知道还有多少天,我想知道现在时间和创造时间之间的差距(距离)是多少天?

<?php
    $response = array();

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    ////ISSET
    if (isset($_POST['projectId'])) {
        $projectId = $_POST['projectId'];

        $result = mysql_query("SELECT *FROM projects WHERE projectId='$projectId'") ;
        if (mysql_num_rows($result)>0) {
            $result = mysql_fetch_array($result);

            $response["title"]=$result["title"];
            $response["exp"]=$result["exp"];
            $response["userIdKarmand"]=$result["userIdKarmand"];
            $response["userIdKarfarma"]=$result["userIdKarfarma"];
            $response["cost"]=$result["cost"];
            $response["zemanat"]=$result["zemanat"];
            $response["time"]=$result["time"];
            $response["createTime"]=$result["createTime"];
            $response["type"]=$result["type"]; 
            $response["nowTime"]=DATE("y-m-d");
            $response["remainTime"]=DATE("y-m-d")-    strtotime($response["createTime"])+$response["time"];
            echo json_encode($response);
        } 
        else {
        }
    }
    else {
   }
?>

没用了。我能做什么?我想比较一下 createtime 以及 nowtime 看看我还有多少时间?

qaxu7uf2

qaxu7uf21#

遍历mysql结果的最佳方法是遍历 fetch_assoc() 功能。

$result = mysql_query("SELECT * FROM projects WHERE projectId = '$projectId'");

// while() through an associative array from the MySQL object
while($result =& $result->fetch_assoc()) {
    $response["title"]                = $result["title"];
    $response["exp"]                  = $result["exp"];
    $response["userIdKarmand"]        = $result["userIdKarmand"];
    $response["userIdKarfarma"]       = $result["userIdKarfarma"];
    $response["cost"]                 = $result["cost"];
    $response["zemanat"]              = $result["zemanat"];
    $response["time"]                 = $result["time"];
    $response["createTime"]           = $result["createTime"];
    $response["type"]                 = $result["type"];
    $response["nowTime"]              = DATE("Y-m-d H:i:s"); // this is how you would need to format dates to work with MySQL
    $remainTime = (strtotime($response['nowTime']) - strtotime($response['createTime'])); // this would return how many seconds have passed since $response['createTime'] until now
    $response["remainTime"]           = $remainTime;
 }
echo json_encode($response);

这将允许你有多少秒,因为 $response['createTime'] 举行在 $remainTime 变量。

3htmauhk

3htmauhk2#

strotime将格式化的字符串日期格式转换为时间戳。不能从字符串数据中减去时间戳数据。必须首先使用strotime()将$response[“remaintime”]转换为timestamp,然后从该值中减去strotime($response[“createtime”])。

相关问题