我有一个简单的表单,它包含两个日期选择器。开始和结束。这个日期选择器显示mm-yy-dddd格式。但我的数据库表包含yyyy-mm-dd格式。现在我想从这个表中获取数据..如何使用mysql和php从这种格式获取数据?
<?php
if(isset($_POST['submitted'])){
// catch data
$startDate = $_POST['start_date'];
$endDate = $_POST['end_date'];
// echo $startDate;
// echo "<br />";
// echo $endDate;
$result = mysql_query('SELECT SUM(amount) AS total FROM tbl_loan WHERE (created BETWEEN "'.$startDate.'" AND "'.$endDate.'") ');
$row = mysql_fetch_assoc($result);
$sum = $row['total'];
}
?>
答案 0 :(得分:1)
使用strtotime
if(isset($_POST['submitted'])){
// catch data
$startDate = $_POST['start_date'];
$endDate = $_POST['end_date'];
// echo $startDate;
// echo "<br />";
// echo $endDate;
$result = mysql_query('SELECT SUM(amount) AS total FROM tbl_loan WHERE (created BETWEEN "'.date("Y-m-d", strtotime($startDate)).'" AND "'.date("Y-m-d", strtotime($endDate)).'") ');
$row = mysql_fetch_assoc($result);
$sum = $row['total'];
}
Strtotime将任何格式的日期字符串转换为unix时间戳。从那里,日期格式将其转换为指定的格式。