<?php
	$timeFirst  = strtotime($stats5backup_day."-".$stats5backup_month."-".$stats5backup_year." ".$stats5backup_hours.":".$stats5backup_minutes.":".$stats5backup_seconds);
	
	$timeSecond = strtotime(date("Y-n-j H:i:s"));
	$differenceInSeconds = $timeSecond - $timeFirst;
	
	$diff = 5 * 60;//5 minute
	
	if($differenceInSeconds>0 && $differenceInSeconds>=$diff)
	{
		$json = file_get_contents('include/db/stats.json');
		$date = json_decode($json,true);
		
		$date['stats5backup']['day'] = date("j");
		$date['stats5backup']['month'] = date("n");
		$date['stats5backup']['year'] = date("Y");
		$date['stats5backup']['hours'] = date("H");
		$date['stats5backup']['minutes'] = date("i");
		$date['stats5backup']['seconds'] = date("s");
		
		foreach($jsondataFunctions as $key => $status)
			if(!in_array($key, array('active-registrations', 'players-debug', 'active-referrals')))
			{
				if($status)
					$date['stats5backup']['stats'][$key] = getStatistics($key);
				else $date['stats5backup']['stats'][$key] = 0;
			}

		$top = [];
		$top = top10players();
		$date['stats5backup']['players'] = [];
		foreach($top as $player)
		{
			$new_player = ['name' => $player['name'], 'level' => $player['level']];
			$new_player['empire'] = get_player_empire($player['account_id']);
			$date['stats5backup']['players'][] = $new_player;
		}

		$top = [];
		$top = top10guilds();
		$date['stats5backup']['guilds'] = [];
		foreach($top as $guild)
		{
			$new_guild = ['name' => $guild['name'], 'ladder_point' => $guild['ladder_point']];
			$new_guild['empire'] = get_guild_empire($guild['master']);
			$date['stats5backup']['guilds'][] = $new_guild;
		}
		
		$jsondataRanking = json_encode($date);
		file_put_contents('include/db/stats.json', $jsondataRanking);
	}
?>