Help converting times!
Hey guys,
I'm really hoping you can help me with this one (it's another, Andy can write PHP but is naff at maths type questions)
I'm creating an appointments system for a website and I need to basically show a representation of a day with 'slots' for every 35 minutes. I've worked out the code to run through the day every 35 minutes but obviously, with it being computer based the minutes become a decimal (i.e. 08:35 becomes 8.583) and that's my problem... I somehow need to convert the decimal back into a 'normal' representation of time. I've been staring at my screen for hours and trying to find something online but I've hit a brick wall.
Any ideas? Anyone?
I've come up with the following code so far, which seems pretty good - I just need to work out a way to get the normal times back out...
<?php
$open = 8.00; // opening time
$close = 16.5; // closing time
$time = $open; // var for processing
$slot = 35; // slot time in minutes
$slot = (1 / 60) * $slot; // $slot mins as decimal
while($time <= ($close - $slot)) {
echo $time . \"<br />\"; // display slot time
$time = $time + $slot; // incriment time
}
?>
this outputs:
8
8.58333333333
9.16666666667
9.75
10.3333333333
10.9166666667
11.5
12.0833333333
12.6666666667
13.25
13.8333333333
14.4166666667
15
15.5833333333
16.1666666667
I have a horrible feeling I'm just being dumb again - I'd really appreciate any help anyone could offer me!
(PS... found this site, which does it in javascript if you're scripting bi-lingual and can convert js into php...)
Thanks!
Andy
Busy posted this at 21:09 — 18th May 2007.
He has: 6,151 posts
Joined: May 2001
You might be better off using the time functions if you plan on adding things like time sheets ...
sprintf("%.2f",$time) would do what you want
andy206uk posted this at 21:26 — 18th May 2007.
He has: 1,758 posts
Joined: Jul 2002
Thanks Busy... not sure if I've done it wrong but that's outputting:
8.00
8.58
9.17
9.75
10.33
10.92
11.50
12.08
12.67
13.25
13.83
14.42
15.00
15.58
8.58 isn't 8 hours 58 minutes. It's 8.58 hours (if you catch my drift)
It's just formatting the numbers to two decimal places... BUT the good news is that I've sussed out the other bit. I just need to take the minutes (i.e 0.583) and multiply them by 60 and I get the proper minutes - then just stitch the time back together again! I'm probably going to use explode(".", $time) to separate the hours and minutes. Is there a more efficient way it can be done?
Andy
andy206uk posted this at 21:31 — 18th May 2007.
He has: 1,758 posts
Joined: Jul 2002
Ps... this is how my test script is looking at the moment:
<?php
$open = 8.00; // opening time
$close = 16.5; // closing time
$time = $open; // var for processing
$slot = 35; // slot time in minutes
$slot = (1 / 60) * $slot; // $slot mins as decimal
while($time <= ($close - $slot)) {
$t = sprintf(\"%.20f\",$time);
$t = explode(\".\",$t);
$t[1] = \"0.$t[1]\" * 60;
$t = implode(\".\", $t);
$t = sprintf(\"%.2f\",$t);
echo $t . \"<br />\"; // display slot time
$time = $time + $slot; // incriment time
}
?>
What do you think? Can it be made more efficient / improved?
Thanks again for the help!
Andy
teammatt3 posted this at 00:20 — 19th May 2007.
He has: 2,102 posts
Joined: Sep 2003
So you're having a hard time taking something like 8.583 and then making it 8:35?
Can't you take the post decimal digits and multiply them by .6?
.583 * .6 = .349 (round it to .35 and take the decimal out)
.50 * .6 = .3
.25 * .6 = .15
How's that? It works for those numbers don't know about the rest though
EDIT: Oh, looks like you solved that already. I need to read the whole post before I reply
Greg K posted this at 04:39 — 19th May 2007.
He has: 2,145 posts
Joined: Nov 2003
Another possibility:
<?php
$open = 8.00; // opening time
$close = 16.5; // closing time
$slot = 35; // slot time in minutes
$slot = $slot / 60; // $slot mins as decimal
for ($time = $open; $time <= ($close - $slot); $time += $slot)
{
$hour = floor($time);
$min = floor(($time - $hour) * 60);
echo $hour . \":\" . str_pad($min,2,'0',STR_PAD_LEFT) . \"<br />\";
}
?>
andy206uk posted this at 11:16 — 19th May 2007.
He has: 1,758 posts
Joined: Jul 2002
Wow greg! That's super efficient code you've written there - I really have to look into 'for' loops - they seem quite handy! Thanks!
kb posted this at 03:15 — 20th May 2007.
He has: 1,380 posts
Joined: Feb 2002
Greg, I just saw this, and then you stole my thunder. You wrote what I was going to
andy206uk posted this at 17:04 — 21st May 2007.
He has: 1,758 posts
Joined: Jul 2002
I've been thinking about this and I don't think the way that I'm doing it is the best way. I'm thinking I might be better converting the times to a time stamp and working in seconds. It's unlikely it'll happen but if one of the venues set their opening time to 8:36 for example it wouldn't work properly as is. I'm going to try doing it another way and hopefully it will be slighly more flexible than my current route...
Thanks for all the help though guys!
Andy
andy206uk posted this at 18:12 — 21st May 2007.
He has: 1,758 posts
Joined: Jul 2002
I'm now using this which is more flexible and probably more efficient:
<?php
//**** set variables ****//
$open = \"0823\";
$close = \"1730\";
$dd = \"25\";
$mm = \"05\";
$yyyy = \"2007\";
$slot = \"35\";
$slot = $slot * 60; // slot in seconds;
//***** create starting and ending timestamps **** //
$startat = mktime(substr($open,0,2),substr($open,2,2),00,$mm,$dd,$yyyy);
$endat = mktime(substr($close,0,2),substr($close,2,2),00,$mm,$dd,$yyyy);
//***** start looping *****//
$time = $startat;
while($time <= ($endat - $slot)) {
echo date(\"H.i\", $time);
echo \"<br>\";
$time = $time + $slot;
}
?>
(in case you wondered... the dd, mm, and yyyy vars will be posted from the previous page in the final thing).
Andy
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.