选择查询以获取php中的d-m-y日期值?

lymnna71  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(221)

在my db entry中,日期列字段具有d-m-y格式的日期值。我需要在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' ");
fxnxkyjh

fxnxkyjh1#

你需要使用 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();

一旦所有内容都测试正常,就可以删除旧列并重命名新列来代替它。

3htmauhk

3htmauhk2#

$time=strottime('16-10-2020')$newformat=date('d-m-y',$time);
不要使用date()格式的“strotime()”,而是按照此模式选择数据库行类型作为date,然后进行比较。

相关问题