Question

How I can convert INT to date in php and than find this date in mysql table ?

Lets say I want to convert

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

And than find that in mysql table

$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 :)";
Était-ce utile?

La solution

Don't pass $month parameter like you do.

Send and pass both $month and $year and handle them after:

$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'");

source

If you can't send both month and year in different variables, do this, like M Khalid Junaid suggested:

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

Autres conseils

Try as below using DateTime::createFromFormat

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

Try this

$input=42014;
$year = strlen(substr($input,-4));
$month = substr(0,(strlen($input)-strlen($year)));
$final = $month."-".$year
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top