选择查询 ro 在 php 中获取 dmY 日期值?

文卡特什

在我的 DB entry_date 列字段中有 dmY 格式的日期值。我需要通过 php 中的过滤器来获取这些日期值。

$from_date= date("d-m-Y", strtotime($from_date));
$to_date = date("d-m-Y", strtotime($to_date));
$res=mysqli_query($con,"select * from sv_customer where entry_date>='$from_date' and entry_date<='$to_date' ");
达曼

您需要使用date类型在数据库中存储日期。不要将它们存储为文本。如果你已经犯了这个错误,那么你需要纠正它。

首先添加一个新的适当的列:

ALTER TABLE `sv_customer`
    ADD COLUMN `entry_date_conv` DATE NULL AFTER `entry_date`;

然后转换数据:

UPDATE sv_customer SET entry_date_conv = STR_TO_DATE(entry_date,'%d-%m-%Y');

现在您可以在 WHERE 子句中使用新列:

$stmt = $con->prepare('select * from sv_customer where entry_date>=? and entry_date<=?');
$stmt->bind_param('ss', $from_date, $to_date);
$stmt->execute();
$res = $stmt->get_result();

一旦测试一切正常,您就可以删除旧列并重命名新列以取代它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章