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
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.