Help with calculating purchases per day

Thanks again for your message.
microtime(true) is hard to decipher, I am wondering if there is another choice, that may work, that’s as easy to read as Y-m-d H:i:s ?

Here’s where the earned amount displays on is the html page:

	<div class="col-md-3">
	<div class="vid_analts_stat" style="background: linear-gradient(135deg, #6abd46 0%,#4CAF50 100%);">
	<svg class="feather" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" data-reactid="461"><line x1="12" y1="1" x2="12" y2="23"></line><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"></path></svg><span>
		<h2>{{LANG today}} {{LANG earnings}}</h2>
		<p>{{TODAY_EARN}}</p>
		</span>
		</div>
		</div>

I look forward to any replies

I’m not sure I understand. Add a comment if you can’t remember what it’s doing. Remember that microtime() and date() functions return different things, and if the place you are using it requires a Unix timestamp, there’s no point giving it a formatted date string.

Can’t help on the other bit, I’m not familiar with whatever template system you are using.

Thanks for your replies.

Regarding, if the ‘earned_amount’ db column populates a decimal place, for example 1.5, instead of just 1, but it won’t display 1.5 on the html page, (and I showed the html code above), that html code gets it’s info from this php code:

$this_day_video_earn = $db->rawQuery("SELECT SUM(earned_amount) AS sum FROM ".T_U_PAID_VIDEOS." c WHERE `time` >= ".$day_start." AND `time` <= ".$day_end." AND user_id_uploaded = ".$pt->user->id);

$today_earn = $this_day_video_earn[0]->sum ;

I’m looking for clues/ideas on what I might modify to get the html to display with a decimal place.

Any additional comments/suggestions are welcomed.

The thing is, this:

<p>{{TODAY_EARN}}</p>

isn’t really html. Well, the open and close paragraph tags are, but the bit enclosed with the double-curly-braces is a directive for whatever template engine you use to display a template variable called TODAY_EARN. That’s what I meant when I said that I’m not familiar with it. If it’s just replacing that string with the variable you passed in, I can’t see why it would fail if there are decimal places. So the next question would be, in here:

$today_earn = $this_day_video_earn[0]->sum ;

does $today_earn have a value in it, when there are decimals, before you pass it through to the next page?

1 Like

Thanks for your replies.
The decimal appears to work now successfully in ‘today_earn’.

But I could use some assistance with the ‘total_earn’ part.

As you can see I substituted ‘amount - admin_com’ with ‘earned_amount’ (and substituted with a new table ‘u_paid_videos’) that works successfully:

$this_day_video_earn = $db->rawQuery("SELECT SUM(earned_amount) AS sum FROM ".T_U_PAID_VIDEOS." c WHERE `time` >= ".$day_start." AND `time` <= ".$day_end." AND user_id_uploaded = ".$pt->user->id);
//$this_day_video_earn = $db->rawQuery("SELECT SUM(amount - admin_com) AS sum FROM ".T_VIDEOS_TRSNS." c WHERE `time` >= ".$day_start." AND `time` <= ".$day_end." AND user_id = ".$pt->user->id);
$today_earn = $this_day_ads_earn[0]->sum + $this_day_video_earn[0]->sum ;

$trans        = $db->where('user_id_uploaded',$user->id)->orderBy('id_user')->get(T_U_PAID_VIDEOS);

It is the ‘total_earn’ part that needs to work with the ‘today_earn’, but I’m not sure what to add (or comment-out) or where to substitute:

$total_earn = 0;
if (!empty($trans)) {
	foreach ($trans as $tr) {
		$video = PT_GetVideoByID($tr->video_id, 0, 0, 2);

		$user_data   = PT_UserData($tr->paid_id);

		$currency         = "";
		$admin_currency         = "";
		$net = 0;
		if ($tr->currency == "USD") {
			$currency     = "$";
			$admin_currency     = "$".$tr->admin_com;
			$net = $tr->amount - $tr->admin_com;
		}

		if ($tr->time >= $start && $tr->time <= $end) {
			$day = date($date_type,$tr->time);
			if (in_array($day, array_keys($array))) {
				$array[$day] += $net;
			}
		}

any suggestions/guidance is appreciated.

Can you expand on that? What is the relationship between the two?

Thanks for your reply.
I am just assuming that each days earnings get totaled to appear as a running total in total_earn.

But currently today_earn displays today’s transaction amounts, but nothing is currently displaying in total earnings

Well, don’t assume anything. Look through all your code and trace what is happening to the $total_earn variable. The code you put in post #25 sets it to zero, but doesn’t appear to do anything else to it. If you are calculating $today_earn for each day inside a loop, you could add that figure to $total_earn each time you go around the loop, after you have calculated each value of $today_earn.

Thanks again for your reply.

I truncated the code in #25 believing that was the part that needed a modification.

Here is the more of that code:

$total_earn = 0;
if (!empty($trans)) {
	foreach ($trans as $tr) {
		$video = PT_GetVideoByID($tr->video_id, 0, 0, 2);

		$user_data   = PT_UserData($tr->paid_id);

		$currency         = "";
		$admin_currency         = "";
		$net = 0;
		if ($tr->currency == "USD") {
			$currency     = "$";
			$admin_currency     = "$".$tr->admin_com;
			$net = $tr->amount - $tr->admin_com;
		}
		else if($tr->currency == "EUR"){
			$currency     = "€";
			$admin_currency     = "€".$tr->admin_com;
			$net = $tr->amount - $tr->admin_com;
		}
		elseif ($tr->currency == "EUR_PERCENT") {
			$currency     = "€";
			$admin_currency = $tr->admin_com."%";
			$net = $tr->amount - ($tr->admin_com * $tr->amount)/100;
		}
		elseif ($tr->currency == "USD_PERCENT") {
			$currency     = "$";
			$admin_currency = $tr->admin_com."%";
			$net = $tr->amount - ($tr->admin_com * $tr->amount)/100;
		}

		if ($tr->time >= $start && $tr->time <= $end) {
			$day = date($date_type,$tr->time);
			if (in_array($day, array_keys($array))) {
				$array[$day] += $net; 
			}
		}

		$total_earn = $total_earn + (float)$net;
		if (!empty($video) && !empty($user_data)) {
			$ads_list   .= PT_LoadPage('transactions/list',array(
				'ID' => $tr->id,
				'PAID_USER' => substr($user_data->name, 0,20),
				'PAID_URL' => $user_data->url,
				'USER_NAME' => $user_data->username,
				'VIDEO_NAME' => substr($video->title, 0,20) ,
				'VIDEO_URL' => $video->url,
				'VIDEO_ID_' => PT_Slug($video->title, $video->video_id),
				'AMOUNT' => $tr->amount,
				"CURRENCY" => $currency,
				"A_CURRENCY" => $admin_currency,
				"NET" => $net,
				"TIME" => PT_Time_Elapsed_String($tr->time)
			));
		}
	}
}
$total_earn = $total_earn + $total_ads;

$pt->array = implode(', ', $array);
$pt->ads_array = implode(', ', $ads_array);
$pt->page_url_ = $pt->config->site_url.'/transactions';
$pt->title       = $lang->earnings . ' | ' . $pt->config->title;
$pt->page        = "transactions";
$pt->description = $pt->config->description;
$pt->keyword     = @$pt->config->keyword;
$pt->content     = PT_LoadPage('transactions/content',array(
	'CURRENCY'   => $currency,
	'ADS_LIST'   => $ads_list,
	'TOTAL_EARN' => $total_earn,
	'TODAY_EARN' => $today_earn,
	'MONTH_EARN' => $month_earn
));

maybe this code
“sets it to zero”, but then does something else? I am not sure.
Any help with this is appreciated.

Well, first have a look through the code and find every line that references $total_earn, then see if you can see what those lines are doing.

Thanks again for your reply.

I have provided the portion of that file that shows every line that references ‘$total_earn’ in #29. There are three.
The first one sets ‘$total_earn’ to zero .

$total_earn = 0;

But, what it does after that, is what I am hoping someone can tell me.

The next one shows what ‘total_earn’ then becomes equal too:

$total_earn = $total_earn + $total_ads;

But, what it does after that, is what I am hoping someone can also explain to me.

and lastly, it ads it all to a list/array? Is that correct?

$total_earn = $total_earn + $total_ads;

Any additional assistance will be very helpful

Four, surely?

You’ve identified the lines that modify the value of that variable, and they’re not complicated, so you can see that it is having the value of $total_ads added to it.

Have a look again at your recent post about passing values from one page to another. I think you’ll see what that last reference to the variable is doing.

So what you need to do next is see what value $total_earn has at each of these points, to see why “nothing is currently displaying in total earnings”.

If the total earnings contain a fraction, could it be the same problem - whatever that was, you said you’d fixed it - that you had with displaying fractional daily earnings?

Thanks again for your reply.
I added a var dump ($total_earn); and see float (0)

I see that ‘total_earn’ is having ‘total_ads’ added to it, which, for testing purposes, ‘total _ads’ is always zero.

I have made sure the transaction is not a fraction. Yet, still see zero for ‘total_earn’.

Can you tell me what this line does:
$video = PT_GetVideoByID($tr->video_id, 0, 0, 2);

Also, because earlier I substituted ‘amount - admin_com’ with ‘earned_amount’, I have tried to substitute it in this section, but am not sure it is correct:

		if ($tr->currency == "USD") {
			$currency     = "$";
			$admin_currency     = "$".$tr->admin_com;
			$net = $tr->amount - $tr->admin_com;

and additional comments/suggestion will be helpful

Is that in each position that $total_earn is altered? Obviously the first reference just sets it to zero, then there are two other references that add separate values to it.

OK, so you know that is always zero, so you can presume for now that is not altering the value.

No. I can presume that it’s retrieving information about a particular video based on its ID (which is in $tr->video_id) based on the function name and the variable name, but I can’t guess what the other three parameters you pass into that function refer to. You’d have to find the function definition for GetVideoByID() and see what they do, or refer to documentation if you have any.

Need more information. Does it produce the correct figures all the time? If so, then it is correct, if not then it is not. I’m not sure how amount - admin_com relates to earned_amount, or how either of those relate to the variables you use in the quoted code.

Thanks for all of your replies.
I have decided that total yearn earnings would be more informative,
so I have added this:

$year_start = $start = strtotime("1 January ".date('Y')." 12:00am");
$year_end = $end = strtotime("31 December ".date('Y')." 11:59pm");
$this_year_video_earn = $db->rawQuery("SELECT SUM(earned_amount) AS sum FROM u_paid_videos c WHERE `time` >= ".$year_start." AND `time` <= ".$year_end." AND user_id_uploaded = ".$pt->user->id);
$year_earn = $this_year_video_earn[0]->sum ;

which displays the correct amount (along with day and month earnings) on the html page, successfully, The only issue is, unlike the displayed day and month amount, the year amount does not show any amount until an amount is added, whereas day and month display a 0 (zero) until an amount is added.

Any help with getting the year_earn to first show a zero, is appreciated.

You could look at using number_format() to format the numbers for display.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.