Jump to content

[SOLVED] convert a string into hh:mm format.


perficut

Recommended Posts

Im trying to work on a small progam, playing around with numbers and that.  One of the features Im trying to get accomplished is converting a variable into hh.mm format.  ex.

$TotalHours=(mins1+mins2);

now I want to echo the $TotalMins to the screen but in a hh:mm format, so if (mins1+mins2) were equal to, say, 19.7532, then it would display 19:45

(Im going to search some more and see if I can find something here already posted about this. Couldnt find anything yet)
Link to comment
https://forums.phpfreaks.com/topic/32363-solved-convert-a-string-into-hhmm-format/
Share on other sites

not sure if there is an easier way to do it like you want it but depending on how your time comes out this should work:

[code=php:0]
$time = explode(".", round("19.7532", 2));
echo $time[0].":".ceil(($time[1] / 100)*60);
[/code]
Looks like it works.  But now I have one more questions.

how do I ad 25% of the tottaltime to itself?

$time = explode(".", round("$totaltime", 2));
echo $time[0].":".ceil(($time[1] / 100)*60);


example output.
First visit will take 4:00 hours
Second visit will take 25% longer so it will echo 5:00 hours
Third visit will take 50% longer so it will echo 6:00 hours
and so on.

If you want to do math on time, you need to convert it to a Unix timestamp (the number of seconds since January 1, 1970).

Using this format, the answer to your first question would be:
[code]<?php
$time = 19.7532;
$x = strtotime(date('Y-m-d')) + ($time * 3600);
echo date('H:i',$x);
?>[/code]

Here's one solution to your second problem:
[code]<?php
$x = 4 * 3600; // 3600 is the number of seconds in an hour
$visit[1] = strtotime(date('y-m-d')) + $x;
$visit[2] = strtotime(date('y-m-d')) + ($x * 1.25);
$visit[3] = strtotime(date('y-m-d')) + ($x * 1.5);
for ($i=1;$i<4;$i++)
  echo 'Visit ' . $i . ': ' . date('G:i',$visit[$i]) . ' hours<br>';
?>[/code]

Ken
So far I have been able to use this routine to get what I need done. Problem Im having is that sometimes it cuts off the mins. Resulting in a display like  3:4  instead of 3:40  sometimes it display it corectly.

$time = explode(".", round("19.7532", 2));
echo $time[0].":".ceil(($time[1] / 100)*60);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.