gpt4 book ai didi

php - 将所选月份与数据库中保存的日期进行比较

转载 作者:行者123 更新时间:2023-11-30 23:25:56 25 4
gpt4 key购买 nike

我的代码的想法是获取表中发生在特定月份和年份的所有更新行,例如更新发生在:2012 年 5 月。因此,我有 2 个显示月份和年份的下拉菜单,我希望当我选择月份和年份时,将显示所选月份和年份发生的所有更新行。有没有人能帮我解决这个问题,我会很高兴的。提示:数据库中存储的日期格式为(Y-m-d)。

最佳答案

提示:不要给我们提示,而是讲述整个故事。

您问题的可能解决方案(适用于 php 5.3+):

<?php
$startDate = date("Y-m-d", strtotime("first day of the month", strtotime( (int) $_POST['year']."-".(int)$_POST['month'].-"01 00:00")));
$endDate = date("Y-m-d", strtotime("last day of the month", strtotime( (int) $_POST['year']."-".(int)$_POST['month'].-"01 23:59")));

$db = ; //your PDO connection

$q = $db->prepare("SELECT `thingstoselect` FROM `yourtable` WHERE `date` > ? && `date` < ?");
$q->execute( array( $startDate, $endDate));

$yourResults = $q->fetchAll();

关于php - 将所选月份与数据库中保存的日期进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13352499/

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