# convert 24 numbers into time of day

convert 24 hours

0 = 12am
.5 = 12:30am

...

12 =12pm
...
22.5=10:30pm
23 =11pm
Ray Paseur

You could be even more accurate if you converted 1440 numbers - that is the number of minutes in a day.

I'll see if I can give you a function that will handle the issue.  Back in a moment...
This will do it for you (mktime will assume today's date, but you can override that - see http://php.net/manual/en/function.mktime.php):

\$h=22.44999;
\$hour=floor(\$h);
\$minute=floor((\$h-\$hour)*60+.01);
\$second=floor((\$h-\$hour-\$minute/60)*60*60+.01);
\$i=mktime(\$hour,\$minute,\$second);
echo \$i . date("r",\$i);

Comment: the +.01 is to ensure the floor doesn't get fooled by small inacuracies in the calculations.
My take on it (written while drinking beer - please make your own tests!)

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

/* PROBLEM DEFINITION
0 = 12am
.5 = 12:30am

...

12 =12pm
...
22.5=10:30pm
23 =11pm
*/

// FUNCTION TO CONVERT A NUMBER TO TIME OF DAY
function num_to_time_of_day(\$x)
{
static
\$times
= array
( '00:00', '00:30'
, '01:00', '01:30'
, '02:00', '02:30'
, '03:00', '03:30'
, '04:00', '04:30'
, '05:00', '05:30'
, '06:00', '06:30'
, '07:00', '07:30'
, '08:00', '08:30'
, '09:00', '09:30'
, '10:00', '10:30'
, '11:00', '11:30'
, '12:00', '12:30'
, '13:00', '13:30'
, '14:00', '14:30'
, '15:00', '15:30'
, '16:00', '16:30'
, '17:00', '17:30'
, '18:00', '18:30'
, '19:00', '19:30'
, '20:00', '20:30'
, '21:00', '21:30'
, '22:00', '22:30'
, '23:00', '23:30'
)
;

// IF BOGUS INPUT DATA
if (!is_numeric(\$x)) return FALSE;

// CONVERT THE NUMBER TO AN ARRAY INDEX
\$n = (\$x * 2);

// RETURN A TIME OR FALSE ON FAILURE
if (isset(\$times[\$n])) return \$times[\$n];
return FALSE;
}

// TEST THE FUNCTION
var_dump( num_to_time_of_day(0) );
var_dump( num_to_time_of_day(0.5) );
var_dump( num_to_time_of_day(3) );
var_dump( num_to_time_of_day(23.5) );
var_dump( num_to_time_of_day('foo') );``````
Best regards, ~Ray
SOLUTION
Chris Stanyon

membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So many ways to skin a cat... But I am wondering why not use the existing clock number that already represents the time of day: Hours, minutes, seconds?  Creating a new time designator feels complicated to me.  But maybe that's just me.

new DateTime() doesn't seem that complicated!
Actually re-reading the question, we've all missed a part - it should be converting to 10:30pm etc, not 22:30.

In my code, just change the return format:

``return \$time->format("g:ia"); //will give 11:30pm etc...``
Some languages represent date time as a floating point - integer = days since day0, fraction gives the h:m:s.u. This is very convenient for date difference and the sort of thing rgb192 was asking.
php doesn't do this, and you can't give something like mktime fractional hours.
Hence the solutions have to do a bit more work.
@ChrisStanyon: Ha!  You're right.  I was, indeed, drinking beer when I read the question ;-)