Milliseconds conversion to basic time - php

I need to convert 1774132 to 30:42 or 30 minutes and 42 seconds or whatever the output of this is. Is there any PHP function that does this?

I found this online a long time ago, but I have no idea where from anymore:
<?php
function secondsToWords($seconds)
{
/*** return value ***/
$ret = "";
/*** get the hours ***/
$hours = intval(intval($seconds) / 3600);
if($hours > 0)
{
$ret .= "$hours hours ";
}
/*** get the minutes ***/
$minutes = bcmod((intval($seconds) / 60),60);
if($hours > 0 || $minutes > 0)
{
$ret .= "$minutes minutes ";
}
/*** get the seconds ***/
$seconds = bcmod(intval($seconds),60);
$ret .= "$seconds seconds";
return $ret;
}
echo secondsToWords(time());
?>

Something like this should work:
printf("%d:%d:%d",$m / (1000*60*60), $m % (1000*60*60) / (1000*60),$m % (1000*60*60) % (1000*60) / 1000 );

Related

How to get the result into a variable?

Might be a kinda noob question.
But I currently have this code:
<?php
$seconds = 1;
$h = (int)($seconds / 3600);
$m = (int)(($seconds - $h*3600) / 60);
$s = (int)($seconds - $h*3600 - $m*60);
if ($h > 1)
echo "$h hours ago";
else if ($m > 1)
echo "$m minutes ago";
else if ($s > 20)
echo "$s seconds ago";
else echo "a moment ago";
$ThisTime = result
?>
Reminder: seconds is a variable that normally gets defined by an other variable.
What I want is:
for instance with "seconds = 1", the output is: "a moment ago". But i want that echo to be
$ThisTime, how can i do this.
Reminder 2: If the input is 400000, the output is: 111 hours ago. So it defines by multiple variables.
Stupid me:
<?php
$seconds = 1;
$h = (int)($seconds / 3600);
$m = (int)(($seconds - $h*3600) / 60);
$s = (int)($seconds - $h*3600 - $m*60);
if ($h > 1)
$Thistime = "$h hours ago";
else if ($m > 1)
$Thistime = "$m minutes ago";
else if ($s > 20)
$Thistime = "$s seconds ago";
else $Thistime = "a moment ago";
?>
EDIT :::::
Ok, i found another problem.
The time is specified in a database.
And it displays there like this: "1407922779"
How can I cahnge this value to be x minutes ago?
Because if i enter that, it says like 30 years ago (something like that).
While it just has been made.
<?php
$seconds = 1;
$h = (int)($seconds / 3600);
$m = (int)(($seconds - $h*3600) / 60);
$s = (int)($seconds - $h*3600 - $m*60);
if ($h > 1) {
$result = "$h hours ago";
} elseif ($m > 1) {
$result = "$m minutes ago";
} elseif ($s > 20) {
$result = "$s seconds ago";
} else {
$result = "a moment ago";
}
$ThisTime = $result;
?>

Need to figure out how to have a countdown in PHP

I have a timestamp and would like to show my users... last sent 1 day, 23 hours, 54 minutes, and 33 seconds ago. I know how to get the difference in time...
$timePast = '2012-08-18 22:11:33';
$timeNow = date('Y-m-d H:i:s');
// gives total seconds difference
$timeDiff = strtotime($timeNow) - strtotime($timePast);
Now I am stuck not being able to show the time like above.
x day, x hours, x mins, x seconds where all the x's should add up to the total seconds time difference. I know the following...
$lastSent['h'] = round($timeDiff / 3600);
$lastSent['m'] = round($timeDiff / 60);
$lastSent['s'] = $timeDiff;
Need you help! Thanks in advance.
After this:
$timeDiff = strtotime($timeNow) - strtotime($timePast);
add:
if ($timeDiff > (60*60*24)) {$timeDiff = floor($timeDiff/60/60/24) . ' days ago';}
else if ($timeDiff > (60*60)) {$timeDiff = floor($timeDiff/60/60) . ' hours ago';}
else if ($timeDiff > 60) {$timeDiff = floor($timeDiff/60) . ' minutes ago';}
else if ($timeDiff > 0) {$timeDiff .= ' seconds ago';}
echo $timeDiff;
Don't do date math manually!
PHP can work out all of the date/time math for you, using the DateTime and DateInterval classes.
Getting an interval between two dates
$timePast = new DateTime('2012-08-18 22:11:33');
$timeNow = new DateTime;
$lastSent = $timePast->diff($timeNow);
// $lastSent is a DateInterval with properties for the years, months, etc.
Formatting example
A function for getting a formatted string might look like the following (though this is only one super-basic way, of many).
function format_interval(DateInterval $interval) {
$units = array('y' => 'years', 'm' => 'months', 'd' => 'days',
'h' => 'hours', 'i' => 'minutes', 's' => 'seconds');
$parts = array();
foreach ($units as $part => $label) {
if ($interval->$part > 0) {
$parts[] = $interval->$part . ' ' . $units[$part];
}
}
return implode(', ', $parts);
}
echo format_interval($lastSent); // e.g. 2 days, 24 minutes, 46 seconds
I took Kalpesh's code and made it work by using floor instead of round and by calculating the different frictions of the day. Here it goes:
function timeAgo ($oldTime, $newTime) {
$timeCalc = strtotime($newTime) - strtotime($oldTime);
$ans = "";
if ($timeCalc > 60*60*24) {
$days = floor($timeCalc/60/60/24);
$ans .= "$days days";
$timeCalc = $timeCalc - ($days * (60*60*24));
}
if ($timeCalc > 60*60) {
$hours = floor($timeCalc/60/60);
$ans .= ", $hours hours";
$timeCalc = $timeCalc - ($hours * (60*60));
}
if ($timeCalc > 60) {
$minutes = floor($timeCalc/60);
$ans .= ", $minutes minutes";
$timeCalc = $timeCalc - ($minutes * 60);
}
if ($timeCalc > 0) {
$ans .= "and $timeCalc seconds";
}
return $ans . " ago";
}
$timePast = '2012-08-18 22:11:33';
$timeNow = date('Y-m-d H:i:s');
$t = timeAgo($timePast, $timeNow);
echo $t;
Output:
1 days, 16 hours, 11 minutes and 18 seconds ago
You'll need a lot of if's, the modulus (%), floor() (not round())
Or Google ;-)

php function convert seconds to military time? [duplicate]

This question already has answers here:
Convert seconds to Hour:Minute:Second
(30 answers)
Closed 11 months ago.
Is there a built-in php function that converts number of seconds to military time?
So it will take 3600 and output 01:00:00.
Try this:
<?php
$seconds = 3600;
echo sprintf("%02d:%02d:%02d",$seconds/3600,($seconds/60)%60,$seconds%60);
?>
$seconds = 3600;
echo gmdate('H:i:s', $seconds);
xato was nearly there.
With this approach it's a little bit of a cheat but I believe it will behave in exactly the way that you want it to with hours, minutes and seconds.
edit: and the behaviour will be consistent across all servers regardless of their TZ settings
$seconds = 3600;
echo date('H:i:s', $seconds);
There you go. I might have a use for such a function myself sometime, so I wrote that for you.
function time_format($time) {
if($time > 86400) {
return "more than 1 day";
}
$display = '';
if ($time >= 3600) {
$hours = floor($time/3600);
$time = $time%3600;
if($hours <= 9) { $display .= "0"; }
$display .= $hours;
} else {
$display .= "00";
}
$display .= ":";
if($time >= 60) {
$minutes = floor($time/60);
$time = $time%60;
if($minutes <= 9) { $display .= "0"; }
$display .= $minutes;
} else {
$display .= "00";
}
$display .= ":";
if($time > 0) {
$seconds = $time;
if($seconds <= 9) { $display .= "0"; }
$display .= $seconds;
} else {
$display .= "00";
}
return $display;
}
EDIT: seeing bozdoz's answer makes me feel deeply ashamed :(

Time difference between php timestamps in hours

I have a php timestamp 1331875634 generated using php time() function.
I have the current timestamp generated using same function.
<?php
$time1 = "1331875634";
$time2 = time();
echo $differencem; //time difference in minutes
echo $differenceh; //time difference in hours
?>
I want to know the difference between these two in minutes. The minutes may be divided by 60 to make it in hours.
You get the different in seconds if you subtract them, so divide it by 60 to get minutes and by 60 again to get hours.
I created this code to take standard PHP UNIX TIMESTAMP, calculate the difference in time and return a standard time or a specialized time format. This is great for timing a project and calculating the time it takes to get the results.
function timerFormat($start_time, $end_time, $std_format = false)
{
$total_time = $end_time - $start_time;
$days = floor($total_time /86400);
$hours = floor($total_time /3600);
$minutes = intval(($total_time/60) % 60);
$seconds = intval($total_time % 60);
$results = "";
if($std_format == false)
{
if($days > 0) $results .= $days . (($days > 1)?" days ":" day ");
if($hours > 0) $results .= $hours . (($hours > 1)?" hours ":" hour ");
if($minutes > 0) $results .= $minutes . (($minutes > 1)?" minutes ":" minute ");
if($seconds > 0) $results .= $seconds . (($seconds > 1)?" seconds ":" second ");
}
else
{
if($days > 0) $results = $days . (($days > 1)?" days ":" day ");
$results = sprintf("%s%02d:%02d:%02d",$results,$hours,$minutes,$seconds);
}
return $results;
}
Example:
$begin_routine_time = time();
echo(timerFormat($begin_routine_time, $time()));
$datetime1 = new DateTime(date('Y-m-d H:i:s', 1331875634));
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$oDiff = $datetime1->diff($datetime2);
echo $oDiff->y.' Years <br/>';
echo $oDiff->m.' Months <br/>';
echo $oDiff->d.' Days <br/>';
echo $oDiff->h.' Hours <br/>';
echo $oDiff->i.' Minutes <br/>';
echo $oDiff->s.' Seconds <br/>';
Once I needed to convert seconds to time like 1 day 03:34:13 days hours:minuts:secondes
I wrote this function
function sECONDS_TO_HMS($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_HMS(65); // 00:01:05
echo sECONDS_TO_HMS(76325); //21:12:05
echo sECONDS_TO_HMS(345872); // 4 Days 00:04:32
I think it could be helpful for you.

Convert number of minutes into hours & minutes using PHP

I have a variable called $final_time_saving which is just a number of minutes, 250 for example.
How can I convert that number of minutes into hours and minutes using PHP in this format:
4 hours 10 minutes
<?php
function convertToHoursMins($time, $format = '%02d:%02d') {
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}
echo convertToHoursMins(250, '%02d hours %02d minutes'); // should output 4 hours 17 minutes
echo date('H:i', mktime(0,257));
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
You can achieve this with DateTime extension, which will also work for number of minutes that is larger than one day (>= 1440):
$minutes = 250;
$zero = new DateTime('#0');
$offset = new DateTime('#' . $minutes * 60);
$diff = $zero->diff($offset);
echo $diff->format('%a Days, %h Hours, %i Minutes');
demo
#Martin Bean's answer is perfectly correct but in my point of view it needs some refactoring to fit what a regular user would expect from a website (web system).
I think that when minutes are below 10 a leading zero must be added.
ex: 10:01, not 10:1
I changed code to accept $time = 0 since 0:00 is better than 24:00.
One more thing - there is no case when $time is bigger than 1439 - which is 23:59 and next value is simply 0:00.
function convertToHoursMins($time, $format = '%d:%s') {
settype($time, 'integer');
if ($time < 0 || $time >= 1440) {
return;
}
$hours = floor($time/60);
$minutes = $time%60;
if ($minutes < 10) {
$minutes = '0' . $minutes;
}
return sprintf($format, $hours, $minutes);
}
$t = 250;
$h = floor($t/60) ? floor($t/60) .' hours' : '';
$m = $t%60 ? $t%60 .' minutes' : '';
echo $h && $m ? $h.' and '.$m : $h.$m;
4 hours and 10 minutes
Sorry for bringing up an old topic, but I used some code from one of these answers a lot, and today I told myself I could do it without stealing someone's code. I was surprised how easy it was. What I wanted is 510 minutes to be return as 08:30, so this is what the code does.
function tm($nm, $lZ = true){ //tm = to military (time), lZ = leading zero (if true it returns 510 as 08:30, if false 8:30
$mins = $nm % 60;
if($mins == 0) $mins = "0$mins"; //adds a zero, so it doesn't return 08:0, but 08:00
$hour = floor($nm / 60);
if($lZ){
if($hour < 10) return "0$hour:$mins";
}
return "$hour:$mins";
}
I use short variable names because I'm going to use the function a lot, and I'm lazy.
The easiest way is :
gmdate('H:i', $numberOfSeconds * 60)
Just in case you want to something like:
echo date('G \h\o\u\r\s i \m\i\n\u\t\e\s', mktime(0, 90)); //will return 1 hours 30 minutes
echo date('G \j\a\m i \m\e\n\i\t', mktime(0, 90)); //will return 1 jam 30 menit
function hour_min($minutes){// Total
if($minutes <= 0) return '00 Hours 00 Minutes';
else
return sprintf("%02d",floor($minutes / 60)).' Hours '.sprintf("%02d",str_pad(($minutes % 60), 2, "0", STR_PAD_LEFT)). " Minutes";
}
echo hour_min(250); //Function Call will return value : 04 Hours 10 Minutes
$m = 250;
$extraIntH = intval($m/60);
$extraIntHs = ($m/60); // float value
$whole = floor($extraIntHs); // return int value 1
$fraction = $extraIntHs - $whole; // Total - int = . decimal value
$extraIntHss = ($fraction*60);
$TotalHoursAndMinutesString = $extraIntH."h ".$extraIntHss."m";
Thanks to #Martin_Bean and #Mihail Velikov answers. I just took their answer snippet and added some modifications to check,
If only Hours only available and minutes value empty, then it will display only hours.
Same if only Minutes only available and hours value empty, then it will display only minutes.
If minutes = 60, then it will display as 1 hour. Same if minute = 1, the output will be 1 minute.
Changes and edits are welcomed.
Thanks. Here is the code.
function convertToHoursMins($time) {
$hours = floor($time / 60);
$minutes = ($time % 60);
if($minutes == 0){
if($hours == 1){
$output_format = '%02d hour ';
}else{
$output_format = '%02d hours ';
}
$hoursToMinutes = sprintf($output_format, $hours);
}else if($hours == 0){
if ($minutes < 10) {
$minutes = '0' . $minutes;
}
if($minutes == 1){
$output_format = ' %02d minute ';
}else{
$output_format = ' %02d minutes ';
}
$hoursToMinutes = sprintf($output_format, $minutes);
}else {
if($hours == 1){
$output_format = '%02d hour %02d minutes';
}else{
$output_format = '%02d hours %02d minutes';
}
$hoursToMinutes = sprintf($output_format, $hours, $minutes);
}
return $hoursToMinutes;
}
2022 answer using Carbon
Carbon::createFromTime(
intdiv($final_time_saving, 60),
($final_time_saving % 60),
0,
0)
->format('H:i')
check this link for better solution. Click here
How to convert hh:mm:ss to minutes
$minutes=$item['time_diff'];
$hours = sprintf('%02d',intdiv($minutes, 60)) .':'. ( sprintf('%02d',$minutes % 60));

Categories