如何在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 :)";
答案 0 :(得分:3)
不要像你那样传递$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'");
答案 1 :(得分:2)
$month = $_GET['month'];
$date = DateTime::createFromFormat('mY',$month);
echo $date->format('m-Y');
答案 2 :(得分:1)
试试这个
$input=42014;
$year = strlen(substr($input,-4));
$month = substr(0,(strlen($input)-strlen($year)));
$final = $month."-".$year