SQL 文本列包含一个日期。如何转换以从文本中获取实际日期?

这是我的代码和我的问题。当我运行 SQL 查询时,它不仅显示当前年份,而且显示前一年(2018 年),它显示(对于当前月份,我正在写这个),我认为我转换日期的方式有问题?任何帮助,将不胜感激。


    <?

$fdaymonth = (new DateTime('first day of this month'))->format('m/d/Y');

$ldaymonth = (new DateTime('last day of this month'))->format('m/d/Y');

$currmonthname = Date('F');

?>

<hr />

<p class="pull-left txt-color-blueLight">Marked in system as sold in <?=$currmonthname;?>: 

<?   

$query = "SELECT source,count(*) as total

FROM apps

WHERE date_sold BETWEEN '" . $fdaymonth . "' and '" . $ldaymonth . "'";


$result = mysqli_query($conn,$query) or die(mysqli_error($conn));

                    if (mysqli_num_rows($result)== 0) echo "<strong>Error</strong>"; 


                    // Print out result

                    while($row = mysqli_fetch_array($result)){


    echo "<b class=\"badge bg-color-greenLight\">" . $row['total'] . "</b>";

}

?>

</p>


皈依舞
浏览 513回答 2
2回答

慕沐林林

我最终添加了一个新列并将 TEXT 列日期转换为 DATE 列以便更轻松地转换。date_sold 列是 TEXT 列,但以 DD/MM/YYYY 格式保存实际日期,这会导致冲突。现在它是一个 DATE 格式的 SQL 列,我可以得到我想要的结果。感谢大家。

慕尼黑5688855

试试这个: CAST($fdaymonth AS DATE) AND CAST($ldaymonth AS DATE)
打开App,查看更多内容
随时随地看视频慕课网APP