This question already has answers here:
Convert number of minutes into hours & minutes using PHP
(14 answers)
Closed 9 years ago.
I wonder if there is a function in php or codeigniter which can do this :
function transformTime($min)
{
$min=(int)$min;
$heure=(int)($min/60);
$minute=(($min/60)-$heure)*60;
return $heure .':' . $minute . ':00';
}
I want convert x minutes to a time format.
Do something like this
<?php
function transformTime($min)
{
$ctime = DateTime::createFromFormat('i', $min);
$ntime= $ctime->format('H:i:s');
return $ntime;
}
echo transformTime(60); // "Prints" 01:00:00
You are almost there, just format the output with sprintf() or str_pad():
function transformTime($min)
{
$hour = floor($min / 60);
$min -= $hour * 60;
return sprintf('%02d:%02d:00', $hour, $min);
}
Related
This question already has answers here:
How to convert a "HH:MM:SS" string to seconds with PHP?
(4 answers)
Closed 5 years ago.
How can I convert a digit time into seconds?
I need to compare the time so I though that it is easier to convert the digit time to seconds and compare the seconds later on.
for example:
00:00:33
00:01:33
02:01:33
You can write a custom parser if you are looking for precise seconds from that format.
Something like this should do the trick :
echo hhmmss2seconds("18:24:35");
function hhmmss2seconds($time) {
$digits = explode(":", $time);
$seconds = 0;
$seconds = $seconds + intval($digits[0]) * 3600; // hours
$seconds = $seconds + intval($digits[1]) * 60; // minutes
$seconds = $seconds + intval($digits[2]); // seconds
return $seconds;
}
You want to use strtotime(). This converts "digit time" to Unix time.
$time_in_seconds = strtotime('00:00:33');
Try this:
echo strtotime("1970-01-01 00:00:11 UTC");
Try This its working
<?php
function time_to_seconds($time) {
list($h, $m, $s) = explode(':', $time);
return ($h * 3600) + ($m * 60) + $s;
}
echo time_to_seconds("00:00:33");
echo time_to_seconds("00:01:33");
echo time_to_seconds("02:01:33");
?>
Thank you..
This question already has answers here:
Convert number of minutes into hours & minutes using PHP
(14 answers)
Closed 6 years ago.
Is there any function I pass minutes and returns hours and minutes in PHP?
I have tried mktime function but not working more than 24 hours see bellow example
echo date('H:i', mktime(0,257));
Try this one
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(500);
this will return output : 08:20
You can use this:
$hours = floor($minutes / 60).':'.str_pad(($minutes % 60), 2, "0", STR_PAD_LEFT);
This question already has an answer here:
Convert Time String to Decimal Hours PHP [closed]
(1 answer)
Closed 8 years ago.
I need little function in PHP which will convert hour:minutes only to hours.
a.g.
15:30 = 15.5
15:15 = 15.25
15:45 = 15.75
and so on
Thank you guys
This should work for you:
<?php
function timeToDecimal($time) {
$hm = explode(":", $time);
return ($hm[0] + ($hm[1]/60));
}
$time = "15:15";
echo timeToDecimal($time);
?>
Output:
15.25
You could do it this way:
function toDecimalTime($time)
{
$parsed = date_parse($time);
return $parsed["hour"] + $parsed["minute"] / 60;
}
This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Closed 8 years ago.
I get the string data from the database
The data format like:
07:00:00.0000000
It means 7:00 am.
How can i convert this string into Date type for comparison.
Therefore, I can get 2 data. Such as
Start:
07:00:00.0000000
End:
16:30:00.0000000
After the time comparison, i can got a answer like 9.3.
You can use strtotime :
$start = strtotime("07:00:00.0000000");
$end = strtotime("16:30:00.0000000");
$difference = $end - $start;
Or with DataTime object :
$start = new DateTime("07:00:00.0000000");
$end = new DateTime("16:30:00.0000000");
$interval = $start->diff($end);
$difference = $end->getTimestamp() - $start->getTimestamp();
Then echo the result :
echo $difference; // in seconds
echo $difference / 60; // in minutes
echo $difference / 3600; // in hours
echo $interval->format('%s') // in seconds
It's then up to your preference. I also suggest you to have a look at this post regarding the performances of the two solutions.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Converting Seconds to HH:MM:SS
I have an integer as a number of seconds. I want to convert that integer into into hours/minutes/seconds like this:
1:45:32
If the number of seconds equates to less than one hour then it should return the string:
45:32
If the number of minutes is less than 10 it should return the string formatted like this:
3:25
And finally if the number of seconds equate to less than 1 minute, it should return the following:
0:04
What is the best way to do this in PHP?
The simpelst approch would be
if($seconds < 3600){
$format = 'i:s';
}else{
$format = 'G:i:s';
}
echo date($format, $seconds);
EDIT: this wouldnt fix your minutes < 10 problem.
you could handle the minutes by itself. like
$time = ($seconds >= 3600) ? date('G', $seconds).':' : '';
$time .= intval(date('i',$seconds)).':'.date('s', $seconds);
<?php
$seconds = (1*60 + 45)*60 + 32; // 1:45:32
define("SECONDS_IN_HOUR", 3600);
define("SECONDS_IN_MINUTE", 60);
// hours
if ($seconds >= SECONDS_IN_HOUR)
{
print floor($seconds/SECONDS_IN_HOUR) . ":";
$seconds = $seconds % SECONDS_IN_HOUR;
}
// minutes
if ($seconds >= SECONDS_IN_MINUTE)
{
print floor($seconds/SECONDS_IN_MINUTE) . ":";
$seconds = $seconds % SECONDS_IN_MINUTE;
}
// seconds
print $seconds;
?>
I think Rufinus is pretty close:
foreach(array(60 => ' 0:s', 3600 => 'i:s', 'G:i:s') as $val => $format)
{
if ($seconds < $val) break;
}
echo ltrim(ltrim(gmdate($format, $seconds), '0'), ' ');
This variant uses a configuration stored inside an array which associates a format string based on a time value in seconds (as key). The last element is the default format that will fall through.
Edit: Unfortunately there is no formatting code in date that allows to specify minutes w/o a leading there. Therefore the date string needs to be re-formatted to remove leading 0's occasionally. It's done with ltrim here.
function formatHMS($time) {
$s = $time % 60;
$time= floor($time/60);
$m = $time % 60;
$time= floor($time/60);
$h = floor($time);
$str = $s;
if ($m>0)
$str = "$m:$str";
if ($h>0)
$str = "$h:$str";
return $str;
}
Consider using explode() & implode() and then apply your logic of less-than & greater-than!