girish.kc Posted September 29, 2010 Share Posted September 29, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/214715-need-help-in-storing-tariff-values/ Share on other sites More sharing options...
girish.kc Posted September 29, 2010 Author Share Posted September 29, 2010 Sorry forgot to mention. I am using PHP and MySQL on Linux to develop this application. Quote Link to comment https://forums.phpfreaks.com/topic/214715-need-help-in-storing-tariff-values/#findComment-1117115 Share on other sites More sharing options...
w3evolutions Posted September 29, 2010 Share Posted September 29, 2010 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"]; } } Quote Link to comment https://forums.phpfreaks.com/topic/214715-need-help-in-storing-tariff-values/#findComment-1117206 Share on other sites More sharing options...
girish.kc Posted September 30, 2010 Author Share Posted September 30, 2010 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. ] Quote Link to comment https://forums.phpfreaks.com/topic/214715-need-help-in-storing-tariff-values/#findComment-1117461 Share on other sites More sharing options...
roopurt18 Posted September 30, 2010 Share Posted September 30, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214715-need-help-in-storing-tariff-values/#findComment-1117483 Share on other sites More sharing options...
girish.kc Posted October 1, 2010 Author Share Posted October 1, 2010 Hi roopurt18, Thanks a lot. It worked very well. Quote Link to comment https://forums.phpfreaks.com/topic/214715-need-help-in-storing-tariff-values/#findComment-1117888 Share on other sites More sharing options...
roopurt18 Posted October 1, 2010 Share Posted October 1, 2010 Glad to hear it. Also keep in mind that when working with financial data it's a good idea to use the arbitrary precision math libraries. Quote Link to comment https://forums.phpfreaks.com/topic/214715-need-help-in-storing-tariff-values/#findComment-1118057 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.