PHP: display 90 min as 1.5 hours - php

Minutes=90
Need to display in hours
echo intdiv($ct['time'], 60).'.'. ($ct['time'] % 60)." Hours";
The output is 1:30 hours.
But i need to display as 1.5 hours.
How to convert the minute into hours? As 1.5, 1.6, 1.7, 1.8, 1.9, 2 hours and so on .

I agree with all the comments saying that it will be clearer to display 1h20 than 1.33 hours. You may could round it and display 1 h and ½ or 1 hour and ¼ but still, it's not easy to read.
If you want to stick to your hours then you could do this:
$min = $cat['prepration_time'];
if ($min < 60) {
echo $min == 1 ? "$min minute" : "$min minutes";
}
else {
$hours = round($min / 60, 1);
echo $hours == 1 ? "$hours hour" : "$hours hours";
}
Examples of output:
1 minute
20 minutes
50 minutes
1 hour
1.2 hours
1.5 hours
1.7 hours

It appears that you have one variable that stores an amount of minutes, $cat['prepration_time'] in your case. To output the minutes as decimal hours all you really need to do is to divide the number of minutes by 60, that's the number of minutes in one hour. In your case that would be
echo $cat['preparation_time'] / 60 . ' hours';
To avoid awkward results like 1.33333333333 hours, you can additionally wrap the calculation in round, where the second parameter of round is the precision, i.e. the number of digits behind the dot.
$minutes = 77;
echo round($minutes / 60, 2) . ' hours'; // 1.28 hours
Be aware however that this format is badly readable and prone to misunderstanding. Does 1.30 hours mean 1 hour and 30 minutes or rather 1 hour and 18 minutes? And while you might be able to convert 1.30 hours in your head, you will surely need a calculator for 1.283 hours. I would recommend to use a commonly understood and unambiguous format, like for example 1 hour and 32 minutes, or 1:32 hours.

Add this code.
Firstly add the hour and minutes after that I have added round function so it will show proper output upto 2 decimal.
$hours =1; $minutes=30 ; $CalculateHours = $hours + round($minutes / 60, 2); echo $CalculateHours . " hours";
Result: 1.5 Hours

Related

How to convert working hours to working days?

I want to display a number of hours in days and hours as a human readable string but I need that 1 day be equal to 7 hours, so a working day.
I found this solution, but based on 1 day equal to 24 hours :
function secondsToTime($seconds) {
$dtF = new \DateTime('#0');
$dtT = new \DateTime("#$seconds");
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
source: Convert seconds into days, hours, minutes and seconds
DateTime won't help here. You need to count it on your own.
But this is simple math. Integer division for days and modulo for hours.
$hours = 12345;
$days = floor($hours/7);
$restHours = $hours%7;
echo "{$days} days, {$restHours} hours"; //1763 days, 4 hours
Custom made code to deal with this (probably on of 1000 ways on can implement this, I don't claim this to be the best, or good or anything like that).
function secondsToTime($seconds) {
$totalMinutes = intval($seconds / 60);
$totalHours = intval($totalMinutes / 60);
$totalDays = intval($totalHours / 7);
echo "$totalDays days and ".($totalHours%7)." hours ".($totalMinutes%60)." minutes and ".($seconds%60)." seconds";
}
e.g.
secondsToTime(8*60*60);
prints
1 days and 1 hours 0 minutes and 0 seconds

Time difference php/mysql need to format the output conditionally (4 small highlights required)

im trying to get difference of date/time from a field type datetime to "right now" using php and mysql as database
this code is working fine, returns the output beautifully ok as required
$datetime1 = new DateTime('mydate1');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%d days %h hours %i minutes');
that is ok so far, no issues as this function is for php 5.3 and i have it on server
my need is 4 small things actually
1) how to eliminate the need for days?
i want to have (25 hours 10 minutes) instead of (1 day 1 hours 10 minutes)
2) how i can make $elapsed be bold or colored if the value is more than 5 hours for example!? simple IF logic will not work as the output is not actually a predefined value...
3) if the days or hours are 0, then want to remove them!
- For example if showing (0 days 10 hours 40 mins) then no need to display the (0 days), should show (10 hours 40 mins) that is enough
- Another example: 0 days 0 hours 45 minutes then to show only "45 minutes" no need for days and hours!
4) if output less than 5 minutes in total (0days 0 hours 1-5mins), then wanna make it show like "a while ago" only no need for any days, hours or minutes... then after 6 minutes.. go like "6 mins"
shortly something like facebook!?
okay, what i searched tried is different combination of workarounds but never worked as you know this interval is for php 5.3 and still seem not widely used?
any hint for one or more parts of this long question is appreciated,
M, Derik
tried to answer all your questions. so read the comments because i didnt write numbers for the problems. sorry
$datetime1 = new DateTime('mydate1');
$datetime2 = new DateTime();
$minutes = round(abs($datetime1 - $datetime2) / 60,2); //to calculate total time in MINUTES
if($minutes < 5) // for awhile ago problem.
{
return "awhile ago";
}
elseif($minutes > 6 && < 60)
{
return $minutes." minutes ago"; //for 6 minutes and after.
}
elseif($minutes>60) // for the hours..
{
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
$string = $hours. " hours and" . $minutes . " minutes ago.";
if($hours>5) //for bolding characters after
{
return "<b>".$hours." hours and".$minutes." minutes ago.</b>"; //for bolding character
}
else
return $hours." hours and".$minutes." minutes ago.";
}
hope this helps.

How to calculate hour:minutes from total minutes?

For example i have 525 minutes, if we will divide it by 60 the result will be 8.75
But 1 hour have only 60 minutes not 75
How can i calculate the exact hour:minutes from total minutes?
$hours = intval($totalMinutes/60);
$minutes = $totalMinutes - ($hours * 60);
Edited to be PHP
This kind of conversion is done using integer division and the modulo operator. With integer division you find out how many of the "large" unit you have and with modulo you find out how many of the "small" unit are left over:
define('MINUTES_PER_HOUR', 60);
$total_minutes = 525;
$hours = intval($total_minutes / MINUTES_PER_HOUR); // integer division
$mins = $total_minutes % MINUTES_PER_HOUR; // modulo
printf("%d minutes is really %02d:%02d.\n", $total_minutes, $hours, $mins);
See it in action.
floor(525 / 60) gives the number of hours (8.75 rounded down to 8).
525 % 60 gives the number of minutes (modulo operator).
What I did for my girl friend made her chart with 60 min intervals eg 1=60,2=120,3=180,4=240,5=300,6=360 etc etc. then I told her to get her minutes eg 337 find the closest number without going over that would be 5 then use the number 5 equals and subtract it from your original minutes 337-300=37 the remainder is the minutes thus 337 minutes equals 5 hours and 37 minutes

Changing hours to days

Editing the question.
I have SQL like this:
`table1`.`DateField` >= DATE_SUB(NOW(), INTERVAL {$days} DAY
Now 24 hours make a whole day. However, what if I want to do the query for the last 3 hours or so?
My table1.DateField is in the format of 2010-03-10 10:05:50.
Original post:
If I have this
1 hour
2 hours
3 hours
..
24 hours
How would I change it to days?
Thanks.
$hours = 80;
$hid = 24; // Hours in a day - could be 24, 8, etc
$days = round($hours/$hid);
if( $days < 0 )
{
echo "$hours hours";
}
else
{
echo "$days days";
}
This assumes you want the hours if it's less than 1 day. If not just remove the switch.
MySQL not only knows DAY as a unit for an interval but also HOUR, MINUTE, ....
see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
$x = 32;
$sql = "SELECT
x,y,z
FROM
foo
WHERE
`table1`.`DateField` >= NOW() - INTERVAL $x HOUR
";
As simple as:
if you want to convert the total of those hour to day:
Just sum the total of hours and that total must be divided by 24
(1 + 2 + 3 + 5) / 24
If you want to convert all of those hours to days:
Just divide by 24 every hours in your list
(1/24) (2/24) (3/24) (5/24)

How can i display seconds ago/minutes ago/with unix timestamp?

I need the unix timestamp - the timestamp i have. Then display the time between like on twitter.
If you have the difference called diff:
$seconds = intval($diff) % 60;
$minutes = intval($diff/60) % 60;
$hours = intval($diff/3600) % 24;
$days = intval($diff/(3600*24));
Is this what you want ?
Not sure what language you need it, but if it will end up in a web page, you may try timeago.
Use example :
echo time_elapsed_string('#1367367755');
echo time_elapsed_string('#1367367755', true);
Output :
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Link to the function.

Categories