I have searched, researched, cursed a lot and shouted at my computer screen repeatedly but nothing seems to work…
I have created the below function to query a mysql database against the current date. If the current date does not exist in the database, the previous days date is generated and the function calls itself again with the new date. This continues until a match is found at which point the function returns an array with the required data.
function getWeekNumber($dateToQuery) {
$oneDay = 86400;
$timestamp = $dateToQuery;
$stringDate = date("d/m/y", $dateToQuery);
include("inc/dbconx.php");
try {
$STH = $DBH->prepare("SELECT `Week_Commencing`,`Week_Number` FROM weeks WHERE `Week_Commencing` = ?");
$STH->bindParam(1,$stringDate);
$STH->execute();
} catch (Exception $e) {
echo "There was a problem retrieving data from the database";
exit;
}
$result = $STH->fetch(PDO::FETCH_ASSOC);
if ($result == FALSE) {
$prevDay = $timestamp - $oneDay;
getWeekNumber($prevDay);
} else {
$output = array("Week_Commencing" => $result['Week_Commencing'], "Week_Number" => $result['Week_Number']);
var_dump($output);
return $output;
}
}
$weekNumber = getWeekNumber($serverTime);
var_dump($weekNumber);
Everything works and you will see where I have included var_dumps to check the results…
The var_dump within the else clause of the function outputs as expected:
array(2) {
["Week_Commencing"]=> string(8) "19/05/14"
["Week_Number"]=> string(1) "4"
}
However, when I call the function it always returns NULL.
Does anyone know why this is happening?
Thanks in advance