所以我有一个问题,你如何在php脚本的查询中使用php date函数。我想以Ymd格式从我的数据库转换sql日期。我不想在我的脚本中更改数据库中的格式。但我不知道该怎么做。我认为问题在于我的语法。任何帮助表示赞赏。
我的剧本:
<?php
require_once('config.php');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
session_start();
$conn = @mysqli_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
$res = $conn -> query("select sum(food.calories) as total_calories, date("Ymd",strtotime(log.dates)) as datum
from foods food inner join foodlog log
on food.id = log.fooditem
where log.user = {$_SESSION['userid']}
group by datum
order by datum");
if ( !$res ) {
die('Wrong query: ' . mysqli_connect_error());
}
$data = array();
for ($x = 0; $x < mysqli_num_rows($res); $x++) {
$data[] = mysqli_fetch_assoc($res);
}
echo json_encode($data);
?>
答案 0 :(得分:0)
MySQL有自己的日期功能。使用他们。在这种情况下,请使用DATE_FORMAT()
。
$res = $conn -> query("select sum(food.calories) as total_calories, DATE_FORMAT(log.dates, '%y%m%d') as datum
from foods food inner join foodlog log
on food.id = log.fooditem
where log.user = {$_SESSION['userid']}
group by datum
order by datum");