【问题标题】:Month and Year with PHP and MySQL?PHP 和 MySQL 的月份和年份?
【发布时间】:2011-08-01 04:17:10
【问题描述】:

我需要在我的 MySQL 数据库中添加一个月份和年份的列。例如,插入条目时,可以是 2010 年 12 月、2009 年 4 月、2011 年 5 月等。

谁能给我一些 SQL 代码,我可以用它来将它插入到我的数据库中?我很困惑是否应该将其存储为 2011-07-30、Unix 时间戳等。

我还需要做以下事情:

  • 月/年下拉(回溯 5 年,提前 5 年)
  • 编辑条目时显示选定的月份/年份
  • 能够在像 2010 年 12 月那样进行搜索时获得结果

如果您还可以提供如何执行上述项目的示例,那就太好了。

【问题讨论】:

    标签: php mysql sql date select-menu


    【解决方案1】:

    对于月份和年份,您可以在数据库字符串中添加以下查询...

    select to_char(date_column_name, 'MONTH YYYY') from table_name
    

    click here了解更多信息

    对于下拉菜单,您可以在那里使用以下条件

    where to_number(to_char(date_column_name,'YYYY')) 
            >= to_number(to_char(curdate,'YYYY') ) -5
    

    click here了解更多信息

    •Be able to get results when doing searches like December 2010
    

    对于最后一个选项,您必须将此文本框值传递给数据库,并希望将此值置于如下条件中-

    select * from table_name 
    where to_char(date_column_name,'MONTH YYYY') = front_end_object_name
    

    【讨论】:

      【解决方案2】:

      你应该插入timestanp,这样你就可以直接将它转换成你想要的任何格式的日期。

      //December 2010
      date('F Y',$timestamp);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-07
        • 1970-01-01
        • 1970-01-01
        • 2012-02-24
        • 2017-01-08
        • 2020-12-29
        • 2016-07-31
        相关资源
        最近更新 更多