PDO::日期参数?

发布于 2024-08-23 21:14:22 字数 307 浏览 4 评论 0原文

是否存在一些可用于日期或时间戳的 PDO::PARAM_???

示例代码:

$sql = "UPDATE my_table SET current_date = :date WHERE id = 43";
$statement = $pdo->prepare ($sql);
$statement->bindValue (":date", strtotime (date ("Y-m-d H:i:s")), PDO::PARAM_STR);
$statement->execute ();

Does some PDO::PARAM_??? exist which can be used for dates or timestamps?

Sample code:

$sql = "UPDATE my_table SET current_date = :date WHERE id = 43";
$statement = $pdo->prepare ($sql);
$statement->bindValue (":date", strtotime (date ("Y-m-d H:i:s")), PDO::PARAM_STR);
$statement->execute ();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

隔纱相望 2024-08-30 21:14:22

在 SQL 查询中写入日期时,您将其写入为字符串;您必须对准备好的语句执行相同的操作,并使用 PDO::PARAM_STR,就像您在建议的代码部分中所做的那样。

对于“时间戳”,如果“时间戳”是指:

  • MySQL 时间戳数据类型:它是相同的,您将其作为 字符串 传递
  • PHP Unix 时间戳,它是一个整数:您将向其传递一个int

When writing a date in an SQL query, you are writing it as a string; you have to do the same with prepared statements, and use PDO::PARAM_STR, like you did in the portion of code you proposed.

And for the "timestamp", if by "timestamp" you mean:

  • The MySQL timestamp data-type: it's the same, you'll pass it as a string
  • The PHP Unix timestamp, which is an integer: you'll pass it an int.
薄荷港 2024-08-30 21:14:22

只需使用 php 日期函数创建日期即可解决此问题。

$handle->execute(array(":date"=>date("Y-m-d H:i:s", strtotime($date)), PDO::PARAM_STR));

编辑:但请注意,strtotime
(http://php.net/manual/en/function.strtotime.php) 无法处理所有
一种日期格式。


Simply creating the date using php date function should fix this issue for you.

$handle->execute(array(":date"=>date("Y-m-d H:i:s", strtotime($date)), PDO::PARAM_STR));

Edit: Please note though, that strtotime
(http://php.net/manual/en/function.strtotime.php) can't handle every
kind of date formats.

逆流 2024-08-30 21:14:22

没有。将日期视为字符串。

Nope. Treat date as a string.

缪败 2024-08-30 21:14:22

您必须将日期视为字符串,但您可以创建一个函数来检查是否是有效日期,然后再将其作为参数传递。
像这样:

function checkValidDate($date, $format = "dd-mm-yyyy"){
            if($format === "dd-mm-yyyy"){
            $day = (int) substr($date,0,2);
            $month = (int) substr($date, 3,2);
            $year = (int) substr($date, 6,4);

        }else if($format === "yyyy-mm-dd"){
            $day = (int) substr($date,8,2);
            $month = (int) substr($date, 5,2);
            $year = (int) substr($date, 0,4);
        }

        return checkdate($month, $day, $year);
}

You have to treat the date as string, but you can create a function to check if is a valid date before pass it as a param.
Like this:

function checkValidDate($date, $format = "dd-mm-yyyy"){
            if($format === "dd-mm-yyyy"){
            $day = (int) substr($date,0,2);
            $month = (int) substr($date, 3,2);
            $year = (int) substr($date, 6,4);

        }else if($format === "yyyy-mm-dd"){
            $day = (int) substr($date,8,2);
            $month = (int) substr($date, 5,2);
            $year = (int) substr($date, 0,4);
        }

        return checkdate($month, $day, $year);
}
黑白记忆 2024-08-30 21:14:22

在 RDBMS 中正确存储 DateTime 值(作为字符串)的完整部分:

/** @const string app_date_format expected date format in the PHP domain (Swiss) */
define( 'app_date_format', 'd.m.Y' ); 

/** @var PDOConnection $db */
$db = new \PDO( $dsn, $db_user, $db_pass, $db_options );

/** @var DateTime $date */
$date = \DateTime::createFromFormat( app_date_format, '30.11.2020' );

$stmt = $db-> prepare(
    "UPDATE `test`
    SET `test_date` = STR_TO_DATE(:date, '%Y-%m-%d %H:%i:%s' )
    WHERE `test`.`test_id` = :id"
);

$id = 1;
$stmt->bindValue( ':id', $id );
$stmt->bindValue( ':date', $date-> format( 'Y-m-d H:i:s'));
$stmt->execute() or die( $stmt-> errorInfo()[2] );

使用 PHP 7.4.25 进行测试;玛丽亚数据库 10.6.4

A complete section to properly store a DateTime value (as a string) in a RDBMS:

/** @const string app_date_format expected date format in the PHP domain (Swiss) */
define( 'app_date_format', 'd.m.Y' ); 

/** @var PDOConnection $db */
$db = new \PDO( $dsn, $db_user, $db_pass, $db_options );

/** @var DateTime $date */
$date = \DateTime::createFromFormat( app_date_format, '30.11.2020' );

$stmt = $db-> prepare(
    "UPDATE `test`
    SET `test_date` = STR_TO_DATE(:date, '%Y-%m-%d %H:%i:%s' )
    WHERE `test`.`test_id` = :id"
);

$id = 1;
$stmt->bindValue( ':id', $id );
$stmt->bindValue( ':date', $date-> format( 'Y-m-d H:i:s'));
$stmt->execute() or die( $stmt-> errorInfo()[2] );

Tested with PHP 7.4.25; MariaDB 10.6.4

洛阳烟雨空心柳 2024-08-30 21:14:22

这对我有用。

//MS SQL
$sql = "UPDATE my_table SET current_date = GETDATE() WHERE id = 43";
$statement = $pdo->prepare ($sql);
//$statement->bindParam (":date", strtotime (date ("Y-m-d H:i:s")), PDO::PARAM_STR);
$statement->execute ();

This worked for me.

//MS SQL
$sql = "UPDATE my_table SET current_date = GETDATE() WHERE id = 43";
$statement = $pdo->prepare ($sql);
//$statement->bindParam (":date", strtotime (date ("Y-m-d H:i:s")), PDO::PARAM_STR);
$statement->execute ();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文