我想知道如何在MySQL和PHP中处理日期。我想知道还剩多少天,我想知道nowTime和createTime之间的差异(距离)是多少天?
<?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
,找出我有多少时间?
答案 0 :(得分:0)
strtotime将格式化的字符串日期格式转换为时间戳。您不能从字符串数据中减去时间戳数据。您必须先使用strtotime()将$ response [“ remainTime”]转换为时间戳,然后从该值中减去strtotime($ response [“ createTime”])。
答案 1 :(得分:0)
通过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
起的秒数。