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
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?

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.