This question already has answers here:
Convert number of minutes into hours & minutes using PHP
(14 answers)
Closed 8 years ago.
I would like to display time in minues as an hour and minutes.
Example 1: I want to display 125 minutes as a 2:05
I know I can to somethink like:
$minutes=125;
$converted_time = date('H:i', mktime(0,$minutes);
This works fine, but if the time is more then 24h it is a problem.
Example 2:
$minutes=1510;
and I want to receive 25:10 (without days), only hours and minutes.
How to do that?
You can use:
$minutes=1510;
$hours = intdiv($minutes, 60).':'. ($minutes % 60);
!!! This only works with php >= v7.xx
Previous answer:
$minutes=1510;
$hours = floor($minutes / 60).':'.($minutes - floor($minutes / 60) * 60);
As simple as that.
$minutes = 125;
$hours = floor($minutes / 60);
$min = $minutes - ($hours * 60);
echo $hours.":".$min;
EDIT: should use floor() instead of round() for getting correct results.
$hours = floor($minutes / 60); // Get the number of whole hours
$minutes = $minutes % 60; // Get the remainder of the hours
printf ("%d:%02d", $hours, $minutes); // Format it as hours:minutes where minutes is 2 digits
Related
This question already has answers here:
PHP: convert seconds to minutes and hours
(2 answers)
Closed 4 years ago.
Convert video time
$json_array = file_get_contents("https://graph.facebook.com/v2.12/$PID[$FID]/videos?fields=title,length,from,description,created_time,source&limit=10&access_token=");
$json_data=json_decode($json_array,true);
foreach($json_data['data'] as $links){
$video_time = $links['length'];
238.142 this time to convert 3.57
Here is the algorithm:
.142 is irrelevant as you only seem to want minutes and seconds
To calculate the seconds, calculate the modulo by 60
238 % 60 = 58
To calculate the minutes, remove the seconds from your total time and divide by 60
(238 - 58) / 60 = 3
=> 3 minutes and 58 seconds
For this you can simply use the gmdate() function,
echo gmdate("H:i:s", 238.142); //output: 00:03:58
echo gmdate("H.i.s", 238.142); //output: 00.03.58
echo gmdate("i.s", 238.142); //output: 03.58
function vtime($lenth){
$seconds = $lenth;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
if($hours == 0){ return "$minutes:$seconds"; }
elseif($minutes == 0){ return $seconds; }
else { return "$hours:$minutes:$seconds"; }
}
echo vtime('304');
So pretty much I'm storing the amount of play time a player has in milliseconds and I need to convert it to the amount of time it equals (string).
I've already tried it but I can't seem to get it to be accurate. I used rounding and it turned out poorly.
Can anybody help me out?
Example: 183547165 -> * days * hours * minutes * seconds
If I'm reading the question right, then I think you want something like this!
<?php
$milliseconds = '183547165';
$time = $milliseconds / 1000;
$days = floor($time / (24*60*60));
$hours = floor(($time - ($days*24*60*60)) / (60*60));
$minutes = floor(($time - ($days*24*60*60)-($hours*60*60)) / 60);
$seconds = ($time - ($days*24*60*60) - ($hours*60*60) - ($minutes*60)) % 60;
echo $days.' days<br>'.$hours.' hours<br>'.$minutes.' minutes<br>'.$seconds.' seconds';
?>
Converts milliseconds to days, hours, minutes, and seconds.
PHP has a date function which do what you want:
date("H:i:s", '183547165');
It outputs:
09:19:25
PHP DATE
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
seconds to minutes and days to weeks
I wonder how I can convert this value to minutes and seconds: 292.96. The value is from this API from Spotify.
Thanks in advance!
Yes, the value is in seconds, so very simple;
int minutes = val / 60;
int seconds = val % 60;
$time = 292.96;
$minutes = floor( $time / 60);
$seconds = $time - ($minutes * 60); // Can add floor() or do mod (%) to round
echo $minutes . ' ' . $seconds; // 4 minutes 52.96 seconds
This question already has answers here:
Convert seconds to Hour:Minute:Second
(30 answers)
Closed 8 years ago.
is there a built in functionality in php to convert seconds to hh:mm:ss
There's no built in function, this will work though:
function formatSeconds($seconds){
$hours = $seconds / (60 * 60);
$minutes = ( $seconds - $hours * 60 * 60) / 60;
$seconds = $seconds - $hours * 60 * 60 - $minutes * 60 - $seconds;
return $hours.':'.$minutes.':'.$seconds;
}
$seconds=3672;
echo date_create()->add(new DateInterval("PT{$seconds}S"))
->diff(new DateTime)
->format('%H:%I:%S');
Just another way that works.
I needed an answer to this recently and I ran across this web page concerning inserting a phpdate into a mysql table. (I bet this is also what you're dealing with.) It seems to indicate that you can use something like
$mysqldate = date( 'H:i:s', time());
This seems to work fine even if the number of seconds exceeds a 24 hour period.
Yes, the strftime() function (manual):
print(strftime("%H:%M:%S", $_seconds_ ))
I need to somehow take a unix timestamp and output it like below
Can this be done with MySQL? Or php
Mike 7s ago
Jim 44s ago
John 59s ago
Amanda 1m ago
Ryan 1m ago
Sarah 1m ago
Tom 2m ago
Pamela 2m ago
Ruben 3m ago
Pamela 5h ago
As you can guess i only wanna print the minute, not minutes and seconds(1m 3s ago)
What should I look into?
Yes it can be done. See related post
$before // this is a UNIX timestamp from some time in the past, maybe loaded from mysql
$now = time()
$diff = $now - $before;
if( 1 > $diff ){
exit('Target Event Already Passed (or is passing this very instant)');
} else {
$w = $diff / 86400 / 7;
$d = $diff / 86400 % 7;
$h = $diff / 3600 % 24;
$m = $diff / 60 % 60;
$s = $diff % 60;
return "{$w} weeks, {$d} days, {$h} hours, {$m} minutes and {$s} secs away!"
}
PHP 5.3 and newer have DateTime objects that you can construct with data coming back from a database. These DateTime objects have a diff method to get the difference between two dates as a DateInterval object, which you can then format.
Edit: corrected sub to diff.
Edit 2:
Two catches with doing it this way:
DateTime's constructor doesn't appear to take a UNIX timestamp... unless prefixed with an #, like this: $startDate = new DateTime('#' . $timestamp);
You won't know what the largest unit is without manually checking them. To get an individual field, you still need to use format, but with just a single code... Something like $years = $dateDiff->format('y');
function sECONDS_TO_DHMS($seconds)
{
$days = floor($seconds/86400);
$hrs = floor($seconds / 3600);
$mins = intval(($seconds / 60) % 60);
$sec = intval($seconds % 60);
if($days>0){
//echo $days;exit;
$hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
$hours = $hrs-($days*24);
$return_days = $days." Days ";
$hrs = str_pad($hours,2,'0',STR_PAD_LEFT);
}else{
$return_days="";
$hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
}
$mins = str_pad($mins,2,'0',STR_PAD_LEFT);
$sec = str_pad($sec,2,'0',STR_PAD_LEFT);
return $return_days.$hrs.":".$mins.":".$sec;
}
echo sECONDS_TO_DHMS(2); // Output 00:00:02
echo sECONDS_TO_DHMS(96000); // Output 1 Days 02:40:00
PHP's date() function
as well as time() and some others that are linked in those docs
This can also be done in Mysql with date and time functions
You can try my Timeago suggestion here.
It can give outputs like this:
You opened this page less than a
minute ago. (This will update every
minute. Wait for it.)
This page was last modified 11 days
ago.
Ryan was born 31 years ago.
I dont have a mysql server at hand, but a combination of the following commands should get you something like what you want.
DATEDIFF
AND
DATEFORMAT