gpt4 book ai didi

PHP时间间隔至少十天

转载 作者:行者123 更新时间:2023-11-29 02:19:07 24 4
gpt4 key购买 nike

这个想法是查询用户最新配置文件更改的最新时间戳。如果他们尝试更新时不到十天,他们将被拒绝。无论我似乎做什么,我都无法正确获得 10 天差异逻辑。 $stamp 是数据库中的 Y-m-d 日期格式。

    <?php
ini_set('display_errors',1);
$json=$_POST['user'];
$json=str_replace('\\','',$json);
$user=json_decode($json);
$pdo=new PDO('mysql://hostname=localhost;dbname=database', 'user', 'password');
$update = $pdo->prepare("update `accounts` set stamp=CURDATE(),`title`='{$user->title}' ,`radio_hanlde`='{$user->radio_handle}' , `carrier`='{$user->carrier}' ,`hometown`='{$user->hometown}' WHERE user_id='{$user->user_id}' AND DATE_DIFF(CURRDATE(),stamp) > 10");
$update->execute();
if ($update->rowCount() != 0 ){
echo json_encode(array('success'=>'1','message'=>'Your profile has been updated successfully!'));}
else {echo json_encode(array('success'=>'0','message'=>'It has been less than ten days since your latest profile change. You will have to wait!'));}
?>

最佳答案

PHP 的 mysqli 和 PDO API 具有检测更新是否影响任何行的函数(分别为 mysqli_affected_rows() 和 PDOStatement::rowCount()),因此您几乎可以丢弃所有代码。

您需要做的就是更新给定用户的所有符合条件的行,例如:

UPDATE accounts
SET stamp = '$stamp'
, title = '{$user->title}'
, radio_hanlde [SIC] ='{$user->radio_handle}'
, carrier = '{$user->carrier}'
, hometown = '{$user->hometown}'
WHERE user_id = '{$user->user_id}'
AND DATEDIFF(CURDATE(),stamp) > 10;

如果 PDOStatement::rowCount() 返回的值不是 0,则打印消息 1,否则打印消息 2。

按照以下示例,由 OP 组装...

$pdo->exec("
UPDATE accounts
SET stamp = '$stamp'
, title = '{$user->title}'
, radio_hanlde ='{$user->radio_handle}'
, carrier = '{$user->carrier}'
, hometown = '{$user->hometown}'
WHERE user_id = '{$user->user_id}'
AND DATEDIFF(CURDATE(),stamp) > 10
");

if (PDOStatement::rowCount() < 1 ){
echo json_encode(array('success'=>'0','message'=>'It has been less than ten days since your latest profile change. You will have to wait!'));
} else {
echo json_encode(array('success'=>'1','message'=>'Your profile has been updated successfully!'));
}

关于PHP时间间隔至少十天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34556646/

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