doukan6564 2014-05-15 22:29
浏览 250

如何从php中的数据库中获取上个月的数据?

I am developing a php website and need to fetch some data from previous month. Not only first previous month but last five months data. And I am confused that how can I do this? In my database I've created two columns. One is "Month" and another is "Year". So row will look like


id ---- text ---- month ---- year

1 ---- hello ---- May ----- 2014

2 ---- hi ---- May ----- 2014


Now I can fetch data by using date() function like this.

$first_month = date('F', strtotime('-1 month'));
$second_month = date('F', strtotime('-2 month'));
$third_month = date('F', strtotime('-3 month'));

for five months, I can create five variables. But for year? I mean if current month is january and year 2014, then I will need to fetch data from December 2013, so how it will work? Can anyone suggest me?

I am so confused. Please tell me if anyone has done something like this before. I need idea or concept.

  • 写回答

3条回答 默认 最新

  • drpkcwwav20524605 2014-05-15 22:33
    关注

    the MySQL date format is YYYY-MM-DD HH:MM:SS

    or (to get the date format for one month ago):

    date("Y-m-d H:i:s",strtotime("-1 month"));
    

    You should be issuing a created (timestamp/datetime) column on new data, and simply searching through this data.

    By creating month, day, year columns and trying to separate it you have only made this harder on yourself.

    评论

报告相同问题?