Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Charges / Billing

Posted on 2013-07-01
Medium Priority
238 Views
Price: 0.09

I need to charge 0.09 for every 30 seconds.

Say total time are 37 seconds. The charges are 0.18

If it's 1-30 seconds, the charge is 0.09

Basically, every 30 seconds, I am supposed to charge 0.09

How do I translate this into PHP code?
0
Question by:rolandmy
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 2

Author Comment

ID: 39289716
I kind of like found a way to do it. Refer to below. Is there a better way of doing it? I kind of feel like I am using the long way to go about it =/

if (\$billing=='xx') {
\$price='0.07';
}else{
\$price='0.09';
}

\$charge = \$durationtime/30;

if ( preg_match("/\./", \$charge) ) {
\$actualcharge = intval(\$charge) + 1;
}else{
\$actualcharge = intval(\$charge) + 0;
}
0

LVL 44

Expert Comment

ID: 39289767
Use the ceil() function to get your 'multiplier', and then simply do the math:

``````\$charge = 0.09;
\$seconds = 39;
\$total = ceil(\$sec/30) * \$charge;

echo \$total
``````
and don't put quotes around your values - they then become strings!
0

LVL 111

Expert Comment

ID: 39289769
Here is how to handle DATETIME information in PHP and MySQL.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

To determine an elapsed time in seconds, create timestamps and subtract.  To determine 30 second intervals or parts of 30 second intervals, use ceil() after dividing the number of seconds by 30.
0

LVL 44

Assisted Solution

Chris Stanyon earned 200 total points
ID: 39289775
Variable mis-match in my previous code:

``````\$charge = 0.09;
\$seconds = 39;
\$total = ceil(\$seconds/30) * \$charge;

echo \$total;
``````
0

LVL 111

Accepted Solution

Ray Paseur earned 1000 total points
ID: 39289823
This seems to test out OK.
http://www.laprbass.com/RAY_temp_rolandmy.php

``````<?php // RAY_temp_rolandmy.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// THE RATE PER PERIOD
\$rate = 0.09;

// THE DURATION OF THE PERIOD IN SECONDS
\$dura = 30;

// THE ELAPSED TIME PERIOD OF SERVICE
\$alpha = 'Today 11:58:03 pm';
\$omega = 'Tomorrow 00:00:17';

// CREATE TIMESTAMPS AND GET SECONDS
\$a_ts = strtotime(\$alpha);
\$z_ts = strtotime(\$omega);
\$secs  = \$z_ts - \$a_ts;

// COMPUTE CHARGES
\$bill = ceil(\$secs/\$dura) * \$rate;

// REPORT THE WORK
echo "FROM \$alpha TO \$omega THERE ARE \$secs SECONDS AND THE CHARGE IS " . '\$' . number_format(\$bill,2);
``````
Best regards, ~Ray
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
###### Suggested Courses
Course of the Month7 days, 1 hour left to enroll