/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Compacting some code

I need some help compacting some code….ok, alot of code, and all for one script 😮 ….the script is a good 200 lines of code, it’s really sad that i couldn’t think of a way to compact it, but i wasn’t able to sensably think of a way to do it….here’s a the beginneg of the code and the end of it, you should be able to figure out what goes in the middle from that.

[code=php]if(date(“G”) == “8” && date(“i”) < “30”) {
$start_time = “1”;
if(date(“i”) < “6”) {
$clock_in = “1.0”;
}elseif(date(“i”) >= “6” && date(“i”) < “12”) {
$clock_in = “1.1”;
}elseif(date(“i”) >= “12” && date(“i”) < “18”) {
$clock_in = “1.2”;
}elseif(date(“i”) >= “18” && date(“i”) < “24”) {
$clock_in = “1.3”;
}elseif(date(“i”) >= “24” && date(“i”) < “30”) {
$clock_in = “1.4”;
}
}elseif(date(“G”) == “8” && date(“i”) >= “30”) {
$start_time = “2”;
if(date(“i”) >= “30” && date(“i”) < “36”) {
$clock_in = “2.5”;
}elseif(date(“i”) >= “36” && date(“i”) < “42”) {
$clock_in = “2.6”;
}elseif(date(“i”) >= “42” && date(“i”) < “48”) {
$clock_in = “2.7”;
}elseif(date(“i”) >= “48” && date(“i”) < “54”) {
$clock_in = “2.8”;
}elseif(date(“i”) >= “54” && date(“i”) < “60”) {
$clock_in = “2.9”;
}
}elseif(date(“G”) == “9” && date(“i”) < “30”) {
$start_time = “3”;
if(date(“i”) < “6”) {
$clock_in = “3.0”;
}elseif(date(“i”) >= “6” && date(“i”) < “12”) {
$clock_in = “3.1”;
}elseif(date(“i”) >= “12” && date(“i”) < “18”) {
$clock_in = “3.2”;
}elseif(date(“i”) >= “18” && date(“i”) < “24”) {
$clock_in = “3.3”;
}elseif(date(“i”) >= “24” && date(“i”) < “30”) {
$clock_in = “3.4”;
}
……….
……….
……….
……….
……….
……….
……….
}elseif(date(“G”) == “17” && date(“i”) >= “30”) {
$start_time = “20”;
if(date(“i”) >= “30” && date(“i”) < “36”) {
$clock_in = “20.5”;
}elseif(date(“i”) >= “36” && date(“i”) < “42”) {
$clock_in = “20.6”;
}elseif(date(“i”) >= “42” && date(“i”) < “48”) {
$clock_in = “20.7”;
}elseif(date(“i”) >= “48” && date(“i”) < “54”) {
$clock_in = “20.8”;
}elseif(date(“i”) >= “54” && date(“i”) < “60”) {
$clock_in = “20.9”;
}
}elseif(date(“G”) == “18” && date(“i”) < “30”) {
$start_time = “21”;
if(date(“i”) < “6”) {
$clock_in = “21.0”;
}elseif(date(“i”) >= “6” && date(“i”) < “12”) {
$clock_in = “21.1”;
}elseif(date(“i”) >= “12” && date(“i”) < “18”) {
$clock_in = “21.2”;
}elseif(date(“i”) >= “18” && date(“i”) < “24”) {
$clock_in = “21.3”;
}elseif(date(“i”) >= “24” && date(“i”) < “30”) {
$clock_in = “21.4”;
}
}elseif(date(“G”) == “18” && date(“i”) >= “30”) {
$start_time = “22”;
if(date(“i”) >= “30” && date(“i”) < “36”) {
$clock_in = “22.5”;
}elseif(date(“i”) >= “36” && date(“i”) < “42”) {
$clock_in = “22.6”;
}elseif(date(“i”) >= “42” && date(“i”) < “48”) {
$clock_in = “22.7”;
}elseif(date(“i”) >= “48” && date(“i”) < “54”) {
$clock_in = “22.8”;
}elseif(date(“i”) >= “54” && date(“i”) < “60”) {
$clock_in = “22.9”;
}
}[/code]

….anyways date(“G”) goes from 8 to 18 and are checked twice(8, 8, 9, 9, 10, 10, ect) and $clock_in and $start_time go from 1 to 22

if ya need more info, please ask lol

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 24.2006 — Do you really want the clock_in numbers to go: 1.0, 1.1, 1.2, 1.3, 1.4, 2.5, 2.6, 2.7, 2.8, 2.9....????? (What happened to 1.5 through 2.4?) Maybe a textual explanation of what this represents would make an algorithm more obvious to us?
Copy linkTweet thisAlerts:
@GenixdeaeauthorJul 24.2006 — I'm tring to replicate our current system of figuring out time for the boat rental....right now we have a punch machine that punches numbers in the format of military time(0-23), and the points change every 6 minutes(6, 12, 18, 24, 30, ect)...so example of 2:30 would be 14.5, 9:30 = 9.5, 4:18 = 16.3, ect... To figure out the charge we take the rental rate * the time out(end time - start time), so example $130.00 * (14.5-10.5 = 4.0), 130 * 4.0 would give us the rate for 4 hours, that explains the x.x....

1.5 to 2.4 went away because our reservation sheet is based on the 30 minute intervals, and the way i have my reservation sheet coded is 1 = 8:00, 2 = 8:30, 3 = 9:00, 4 = 9:30, ect. So in turn 1.5 to 1.9 wont exist because 1 only goes till 8:29(.0 to .4), then it turns to 2, and 2.0 to 2.4 wont exist because 2 starts at 8:30(.5 to .9)...

that make a little more sense?
Copy linkTweet thisAlerts:
@GenixdeaeauthorJul 24.2006 — i couldn't fall asleep, so i through together what i had thought of for compacting the code...here it is[code=php]$start_time = ""; //declare the variable
//Lets get a for loop from start to end
for($i = "8"; $i <= "18"; $i++) {
if(date("G") == $i) { //find out what time it is, so we can determine what number to put(1, 2, 3, 4, ect)
if(date("i") < "30") {//is it prior to the half-hour?
$start_time = ""; //Right here is where i am having troubles figuring out what to do....this would be 1, 2, 3, 4, 5....up to 22 so it works with my reservation sheet
if(date("i") < "6") {
$clock_in = $start_time .".0";
}elseif(date("i") >= "6" && date("i") < "12") {
$clock_in = $start_time .".1";
}elseif(date("i") >= "12" && date("i") < "18") {
$clock_in = $start_time .".2";
}elseif(date("i") >= "18" && date("i") < "24") {
$clock_in = $start_time .".3";
}elseif(date("i") >= "24" && date("i") < "30") {
$clock_in = $start_time .".4";
}
}elseif(date("i") >= "30") { //we're outside the first half-hour
$start_time = ""; //once again this is where i'm having the problem, same situation as above
if(date("i") >= "30") {
$clock_in = $start_time .".5";
}elseif(date("i") >= "36" && date("i") < "42") {
$clock_in = $start_time .".6";
}elseif(date("i") >= "42" && date("i") < "48") {
$clock_in = $start_time .".7";
}elseif(date("i") >= "48" && date("i") < "54") {
$clock_in = $start_time .".8";
}elseif(date("i") >= "54" && date("i") < "60") {
$clock_in = $start_time .".9";
}
}
}
}[/code]
whether this will work or not i'm not sure, but it's all that i could come up with
Copy linkTweet thisAlerts:
@NogDogJul 24.2006 — [code=php]
function get_clock_in($time)
{
$hour = date("G", $time);
if($hour >= 8 and $hour <= 18)
{
$dec = floor(date("i", $time) / 6);
$int = floor(($hour + $dec/10 - 7.5) * 2);
$interval = sprintf("%d.%d", $int, $dec);
}
else
{
$interval = FALSE;
}
return($interval);
}
// SAMPLE USAGE:
$clock_in = get_clock_in(time());
[/code]
Copy linkTweet thisAlerts:
@GenixdeaeauthorJul 24.2006 — Thanks m8, that did the trick
×

Success!

Help @Genixdeae spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...