So I just started with Coding like 2 weeks ago and I Need to make a function that gives back how much time ago a given date was but only in months and years together with two textfields to type the time in and a submit button and I have something but it doesnt quite work and I think I made mistakes aswell.
I want to be able to type in year and month into the textfields and when Pressing submit have them go through the function which then gives out the time ist been since the given date in numbers like "it's been 03 months and 03 years"
<?php
function timeago(int $month, int $year)
{
$month = date('m') - $month;
$year = date('Y') - $year;
$year = sprintf("%02d", $year);
$month = sprintf("%02d", $month);
$since = array($month, $year);
return $since;
}
echo '<form method="post" action="">
Month: <input type="text" name="month" maxlength="2" /><br>
Year: <input type="text" name="year" maxlength="4" /><br>
<input type="submit" name="btn_submit" value="submit" />
</form>';
echo $since;
echo timeago($_POST['month'], $_POST['year']);
?>
This is what I get so far:
Notice: Undefined variable: since in C:\Users\Documents\PHP Files\test.php on line 27
Notice: Undefined index: month in C:\Users\Documents\PHP Files\test.php on line 28
Notice: Undefined index: year in C:\Users\Documents\PHP Files\test.php on line 28
Fatal error: Uncaught TypeError: Argument 1 passed to timeago() must be of the type int, null given, called in C:\Users\Documents\PHP Files\test.php on line 28 and defined in C:\Users\Documents\PHP Files\test.php:2 Stack trace: #0 C:\Users\Documents\PHP Files\test.php(28): timeago(NULL, NULL) #1 {main} thrown in C:\Users\Documents\PHP Files\test.php on line 2