Jump to content

I think i might be doing something wrong?


DeanWhitehouse

Recommended Posts

First off code :D

 

<?php
function FormatTimeDiff($t1,$t2 = null,$format = 'yfwdhms')
{
$t2 = $t2 === null ? time() : $t2;
$s = abs($t2 - $t1);
$sign = $t2 > $t1 ? 1 : -1;
$out = array();
$left = $s;
$format = array_unique(str_split(preg_replace('`[^yfwdhms]`', '', strtolower($format))));
$format_count = count($format);
$a = array('y'=>31556926, 'f'=>2629744, 'w'=>604800, 'd'=>86400, 'h'=>3600, 'm'=>60, 's'=>1);
$i = 0;
foreach($a as $k=>$v)
{
	if(in_array($k, $format))
	{
		++$i;
		if($i != $format_count)
		{
			$out[$k] = $sign * (int)($left / $v);
			$left = $left % $v;
		}
		else
		{
			$out[$k] = $sign * ($left / $v);
		}
	}
	else
	{
		$out[$k] = 0;
	}
}
return $out;
} 

if(isset($_GET['account']))
{
$id = mysql_real_escape_string(((int) $_GET['account']));
$sql = "SELECT interest,name,maturation FROM nbanking_types WHERE id = '".$id."'"; 
$sql = mysql_query($sql);
if(mysql_num_rows($sql) == 0)
{
	echo "Account type Not Found";
}
else
{
	$account = mysql_fetch_assoc($sql);

	$sql = "SELECT last_maturation,balance FROM user_nbanking WHERE user_id = '".mysql_real_escape_string($_SESSION['user_id'])."' AND package_id = '".$id."'";
	$sql = mysql_query($sql);
	if(mysql_num_rows($sql) == 0)
	{
		mysql_query("INSERT INTO user_nbanking (user_id,package_id) VALUES ('".mysql_real_escape_string($_SESSION['user_id'])."','".$id."')");
		$sql = mysql_query($sql);
	}

	$user_account = mysql_fetch_assoc($sql);

	$next_maturation = date("Y-m-d H:i:s",strtotime($user_account['last_maturation']." + ".$account['maturation']." days"));//Next maturation date

	if($next_maturation <= date("Y-m-d"))
	{
		mysql_query("UPDATE user_nbanking SET balance = '".($user_account['balance'] + ($user_account['balance'] / 100 * $account['interest']))."', last_maturation = NOW() WHERE WHERE user_id = '".mysql_real_escape_string($_SESSION['user_id'])."' AND package_id = '".$id."'") or die(mysql_error());
		$sql = mysql_query($sql);
		$user_account = mysql_fetch_assoc($sql);
		$next_maturation = date("Y-m-d H:i:s",strtotime($user_account['last_maturation']." + ".$account['maturation']." days"));//Next maturation date
	}

	$time_dif = FormatTimeDiff(strtotime($user_account['last_maturation']),strtotime($next_maturation));

	$new_time = "";

	if($time_dif['y'] > 0)
		$new_time .= " ".$time_dif['y'].($time_dif['y'] > 1 ? " years" : " year");
	if($time_dif['f'] > 0)
		$new_time .= " ".$time_dif['f'].($time_dif['f'] > 1 ? " months" : " month");
	if($time_dif['w'] > 0)
		$new_time .= " ".$time_dif['w'].($time_dif['w'] > 1 ? " weeks" : " week");
	if($time_dif['f'] > 0)
		$new_time .= " ".$time_dif['f'].($time_dif['f'] > 1 ? " months" : " month");
	if($time_dif['d'] > 0)
		$new_time .= " ".$time_dif['d'].($time_dif['d'] > 1 ? " days" : " day");
	if($time_dif['h'] > 0)
		$new_time .= " ".$time_dif['h'].($time_dif['h'] > 1 ? " hours" : " hour");
	if($time_dif['m'] > 0)
		$new_time .= " ".$time_dif['m'].($time_dif['m'] > 1 ? " minutes" : " minute");
	if($time_dif['s'] > 0)
		$new_time .= " ".$time_dif['s'].($time_dif['s'] > 1 ? " seconds" : " second");
}
?>

 

Quick sum up of the code, the above code should get certain user account details from their account and work out when they are due to be added interest to their balance and if they are add it and set the last paid day to this day.

 

Now i know for a fact that the code is flawed (although only one way) and that is that if the user doesn't log in for a few days then they will have gaps between interest payment.

 

What ways would you recommend around this without cron jobs.

 

Also i think the script is bugged in its logic.

 

One other thing i tried making the day between the interest payments 0 and i didn't gain a payment, can anyone see why.

 

I am tired so i might of missed things, i just need another head to work on this :)

 

edit: sql dumps

This is the nbanking_types table (a row in it)

id      interest  maturation  name

1  17.5        1        High Interest

 

and this is the user banking one

User Id  Package ID  Balance    ID    Last Maturation

2        1        50      1      2009-01-28 18:07:54

 

Thanks,

Blade

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.