Jump to content

[SOLVED] Work out year status


DeanWhitehouse

Recommended Posts

I am trying to find out whether a student is still on a course with this function i wrote

<?php
function work_out_yr_status($started,$duration)
{
$started = strtotime($started);
$start_month = date("n",$started);
$start_year = date("Y",$started);

$current_month = date("n");
$current_year = date("Y");

$yr_duration = $duration;

$end_month = 6;

$yr_difference = $current_year - $start_year;

for($i = 0;$i <= $yr_duration;$i++)
{
	if($yr_difference > $yr_duration && $current_month > $end_month)
	{
		echo "Course completed in ";
		echo $start_year + $yr_duration;
	}
	elseif($yr_difference == $i && $current_month >= $start_month)//first academic year and before new year
	{
			echo "first academic year and before new year";
	}
	elseif($yr_difference == $i && $current_month <= $end_month)
	{
		echo "first academic year and after new year";
	}
}
}
?>

How ever this prints out "first academic year and before new year", which i think is correct, but i need it to do this

Year 1 - completed

 

Year 2 - in progress

 

Year 3 - not started

This is a for loop i use

<?php
for($i = 1;$i <= 2;$i++)
{
work_out_yr_status("2007-09-05 19:31:11",2);

echo " Year ".$i."<hr align=\"center\">";

$member -> get_program_units($id,$i);

echo "<br><br>";
}
?>

It prints out

first academic year and before new year Year 1

 

first academic year and before new year Year 2

Obviously the text and display i can do , but i can't think of the logic for the function.

 

Any help please ;)

Link to comment
https://forums.phpfreaks.com/topic/135703-solved-work-out-year-status/
Share on other sites

Your for loop only goes twice, so the year 3 is impossible to print out right now.

 

<?php
function work_out_yr_status($started,$duration)
{
   $started = strtotime($started);
   $start_month = date("n",$started);
   $start_year = date("Y",$started);
         
   $current_month = date("n");
   $current_year = date("Y");

   $yr_duration = $duration;
   
   $end_month = 6;
   
   $yr_difference = $current_year - $start_year;
      
   for($i = 0;$i <= $yr_duration;$i++)
   {
      if($yr_difference > $yr_duration && $current_month > $end_month)
      {
         echo "Course completed in ";
         echo $start_year + $yr_duration;
      }
      elseif($yr_difference == $i && $current_month >= $start_month)//first academic year and before new year
      {
            echo "in progress";
      }
      elseif($yr_difference == $i && $current_month <= $end_month)
      {
         echo "completed";
      }else {
          echo "not started";
      }
   }
}
?>

 

I think that is what you are looking for?

Lol, sorry no.  i put in static numbers so it would be easier to understand the actual values are dynamic.

 

I believe i have fixed it, this is the current function

<?php
function work_out_yr_status($started,$duration,$i)
{
$started = strtotime($started);
$start_month = date("n",$started);
$start_year = date("Y",$started);

$current_month = date("n");
$current_year = date("Y");

$yr_duration = $duration;

$end_month = 6;

$yr_difference = $current_year - $start_year;

	if($yr_difference > $yr_duration && $current_month > $end_month)
	{
		echo "Course completed in ";
		echo $start_year + $yr_duration;
	}
	elseif($yr_difference == $i && $current_month >= $start_month || $yr_difference == $i && $current_month <= $end_month)
	{
		echo "In progress";
	}
	else
		echo "Not started";
}
?>

And my loop to run it

<?php
for($i = 1;$i <= $program[0]['length'];$i++)
{
work_out_yr_status($details['started'],$program[0]['length'],$i);

echo "Year ".$i."<hr align=\"center\">";

$member -> get_program_units($id,$i);

echo "<br><br>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.