gpt4 book ai didi

php - php 日期选择器的错误处理

转载 作者:行者123 更新时间:2023-11-29 12:40:25 26 4
gpt4 key购买 nike

你好,我该如何解决这个问题?例如,用户选择 future 的日期,例如今天是 10 月 5 日,用户输入 10 月 20 日,然后他选择 $dateto 为过去的日期,也许是 9 月 30 日

我如何阻止用户做这样的事情?

这是我的代码

<?php
if(isset($_GET['datefrom']) && ($_GET['dateto'])){
$datefrom = $_GET['datefrom'];
$dateto = $_GET['dateto'];
$qry = "SELECT sum(order_detail.quantity*order_detail.price) as chenes, orders.date
FROM order_detail
LEFT JOIN orders
ON order_detail.orderid=orders.serial
WHERE date(orders.date) BETWEEN '$datefrom' AND '$dateto'";
}
else {
$qry = "SELECT sum(order_detail.quantity*order_detail.price) as chenes, orders.date
FROM order_detail
LEFT JOIN orders
ON order_detail.orderid=orders.serial";
}


mysql_set_charset("UTF8");
$result = @mysql_query($qry);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while ($row=mysql_fetch_array($result)){

//echo $datefrom." ";
//echo $dateto." - ";
echo "₱".number_format($row['chenes'],2);

}
?>

$datefrom 是过去,$dateto 是现在或仍然是过去,但不超过 $datefrom。

$datefrom 应始终早于 $dateto。

请帮帮我,谢谢。 :)

最佳答案

我处理它的方式(如果在这之前我不通过 JS 处理它)将是:

// This puts both dates into a date format PHP can understand
$tmpFrom = strtotime($_GET['datefrom']);
$tmpTo = strtotimem($_GET['dateto']);

// Compare the dates, if TO is after (greater) than FROM, everything's fine. Otherwise, Swap 'em.
$dateFrom = ($tmpTo - $tmpFrom ) > 0 ? $_GET['datefrom'] : $_GET['dateto'];
$dateTo = ($tmpTo - $tmpFrom ) > 0 ? $_GET['dateto'] :$_GET['datefrom'];

关于php - php 日期选择器的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26204403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com