将Mysql文本转换为日期

时间:2016-11-29 15:23:40

标签: php mysql sql

我目前正在使用具有月份列和年份列的数据库。两者都是文本类型。月份存储为' 1月'和年份按照您的预期存储,' 2016' ..

有关连接这些内容并将其转换为日期类型的建议吗?

3 个答案:

答案 0 :(得分:1)

您可以使用这样的查询。您只需将字符串更改为您的字段名:

<强>样品

else if(method.equals("send"))
        {

            String teamone = params[1];
            String teamonepts = params[2];
            String teamtwo = params[3];
            String teamtwopts = params[4];
            try {
                URL url = new URL(send_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                String data = URLEncoder.encode
                        ("teamone", "UTF-8")+"="+ URLEncoder.encode(teamone, "UTF-8")+"&"+
                        URLEncoder.encode("teamonepts", "UTF-8")+"="+ URLEncoder.encode(teamonepts, "UTF-8")+"&"+
                        URLEncoder.encode("teamtwo", "UTF-8")+"="+ URLEncoder.encode(teamtwo, "UTF-8")+"&"+
                URLEncoder.encode("teamtwopts", "UTF-8")+"="+ URLEncoder.encode(teamtwopts, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                httpURLConnection.disconnect();

                return "Data Sent";

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
        return null;
    }

<强>结果

select STR_TO_DATE(concat('2016',' ','April','1'), '%Y %M %D');

答案 1 :(得分:0)

这对我有用:

select convert(date,(concat('January',' ','2016')))

答案 2 :(得分:0)

    $months = [
    'january'   => 1,
    'february'  => 2,
    'march'     => 3,
    'april'     => 4,
    'may'       => 5,
    'june'      => 6,
    'july'      => 7,
    'august'    => 8,
    'september' => 9,
    'october'   =>10,
    'november'  =>11,
    'december'  =>12
    ];

    $year       = 2016;
    $month_text = 'january';
    $day        = 1;

    if($months[strtolower($month_text)]<10){
       $month = '0'.$months[strtolower($month_text)];
    }else{
       $month = $months[strtolower($month_text)];
    }
    if($day<10){
       $day = '0'.$day;
    }else{
       $day = $day;
     }


     echo $year.'-'.$month.'-'.$day;

使用类型日期在db中创建一个新的feld并将其插入到db中。也许将它包装在一个函数中并将其放入while循环中。?