Jump to content

Recommended Posts

I am working on a application which has an option to set the time based tariff for the usage. The time interval is 15 mins. So the there will be 96 [24*4] values for a given day

[00:00 to 00:15, 00:15 to 00:30, 00:30 to 00:45, 00:45 to 01:00 ....... 23:45 to 00:00]

 

As of now I'm storing these values as

{"00:00 - 01:00":"2","00:00 - 00:15":"0.00","00:15 - 00:30":"0.00","00:30 - 00:45":"0.00","00:45 - 01:00":"0.00", , , , , , , , ,"23:45 - 00:00":"0.00"}

 

But finding it difficult while generating the report :-\.  In report I have to check the usage time falls in which time interval and apply the rate defined for that interval.

 

Help needed urgently...

 

Link to comment
https://forums.phpfreaks.com/topic/214715-need-help-in-storing-tariff-values/
Share on other sites

The code you put up as how your storing it looks like JSON as apposed to a PHP array. It may be easier to store them in the following manner:

 


$startEndTime = "45"; //Seconds
$rateToUse = array(); // This is an array as 45 sec can fall into 0-60, 30-45

$times["0-60"]["Rate"]=2;
$times["0-15"]["Rate"]=0;
$times["15-30"]["Rate"]=0;
$times["30-45"]["Rate"]=0;

foreach($times as $interval=>$rate) {
     $startEndTime = split($interval,'-');
     if($timeTocheck >= $startEndTime[0] && $timeToCheck <= $startEndTime[1]) {
          $ratesToUse[] = $rate["Rate"];
     }
}

Thanks for the replay.

 

Yes I'm using the JSON to store the values.

I can't use the following code.

 

$times["0-60"]["Rate"]=2;

$times["0-15"]["Rate"]=0;

$times["15-30"]["Rate"]=0;

$times["30-45"]["Rate"]=0;

 

Because I have to use the hour along with the minutes. [ Tariff rate for 00:00 - 00:15 may be different from 01:00 - 01:15. ]

I would store the intervals as:

id | start | rate

 

Where "start" is the first second of the interval.  Each interval is therefore 900 seconds long and they are as following:

0 - 899        00:00 to 00:14:59

900 - 1799        00:15 to 00:29:59

1800 - 2699      00:30 to 00:44:59

 

Then you can load an array of "start" => "rate"

<?php
// Note that you'll have to use appropriate database functions
$rates = array();
$q = query( "select start, rate from the_table order by start" );
while( $r = fetch_object( $q ) ) {
    $rates[ $r->start ] = $r->rate;
}
?>

 

Now to determine the tax rate for a given timestamp:

<?php
// some datetime
$dt = '2010-09-01 16:23:42';
// convert to timestamp
$ts = strtotime( $dt );
// determine seconds of day
$seconds = 3600 * ((int)date( 'H', $ts )) + 60 * ((int)date( 'i', $ts )) + ((int)date( 's' ));
// now determine which interval it starts in
$interval = $seconds - ($seconds % 900);
// look up
echo 'rate is: ' . $rates[ $interval ];
?>

 

That's not thoroughly thought through but it might get you there.

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.