MyBB Community Forums

Full Version: Tasks Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am currently trying to develop a task that will check if a user is over the age of 18 and, if they are, add them to an additional group. Unfortunately, it doesn't work and doesn't give me an error. Could someone have a look and see if they can help me find out what is wrong?

Code: http://pastebin.com/sgddNLDc

 I have made sure the class is task_(name of document), but I can't find any tutorials or anything.

Any help is appreciated.
$age = date_diff(date_create($formatted_dob), date_create("today"))->y;
needs to be
$age = date_diff(date_create($formatted_dob), date_create())->format('%y');

That should at least give you a proper numeric value for $age. date_create() with no parameters defaults to the current date/time so 'today' is unnecessary.
Ammended that, but still doesn't work. Thanks for the help.
fetch_query is not a function of the mybb's database class.
I changed your code with mybb database functions:
<?php
 
 
function task_agecheck($task)
{    
	global $mybb, $db, $lang, $cache, $plugins;
 
	$query = $db->simple_select("users", "*", "additionalgroups NOT LIKE '%9%'");
	$counter = 0;
	   
	while($user = $db->fetch_array($query))
	{
		if ($user['birthday'] == "")
		{
			continue;
		}
		$dob = explode("-", $user['birthday']);
		$formatted_dob = $dob[2] . "-" . $dob[1] . "-" . $dob[0];
		$age = date_diff(date_create($formatted_dob), date_create())->format('%y');
		echo $age;
		if ($age >= 18)
		{
			$counter++;
			$update = array();
			if ($user['additionalgroups'] == "")
			{
				$update["additionalgroups"] = '9';
			} else
			{
				$update["additionalgroups"] = $user['additionalgroups'].',9';
			}
			$db->update_query("users", $update, "uid=".$user['uid']);
		}
	}
   
	$cache->update_moderators();
	add_task_log($task, "User age check completed successfully. " . $counter . " users were promoted");
}    
?>
Thanks so much for fixing it!

Would you happen to know if there are some tutorials or documentation on this?
Here you can find database methods:
https://docs.mybb.com/1.8/development/pl...e-methods/