MySQL-将INT转换为DATE格式


MySQL - Convert INT to DATE format.

如何在php中将INT转换为日期,然后在mysql表中找到这个日期?

比方说我想转换

$month = (int)$_GET['month'];
$month = 42014 ;//This means 04th month in 2014 year
$month = 04-2014; // I want this

然后在mysql表中发现

$query=mysql_query("SELECT id FROM matches WHERE date=$month");// This need to select
                                                               // all data that is
                                                               // created in 04th month
                                                               // of 2014 year .

echo "Thanks :)";

不要像您那样传递$month参数。

发送并通过$month$year,并在之后进行处理

$month = (int)$_GET['month'];
$year = (int)$_GET['year'];
if($month < 10) { // add this check for months lesser then october (they containt 1 digit, which is wrong for t-sql)
    $month = "0".$month;
}
$query=mysql_query("SELECT id FROM matches WHERE DATE_FORMAT(datefield, '%m-%Y') = '$month-$year'");

如果你不能用不同的变量同时发送月份和年份,请按照M Khalid Junaid的建议这样做:

$query=mysql_query("SELECT id FROM matches WHERE DATE_FORMAT(datefield,'%m%Y')='$month'");

使用DateTime::createFromFormat进行如下尝试

$month = $_GET['month'];
$date = DateTime::createFromFormat('mY',$month);
echo $date->format('m-Y');

试试这个

$input=42014;
$year = strlen(substr($input,-4));
$month = substr(0,(strlen($input)-strlen($year)));
$final = $month."-".$year