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..
Related
I wish to multiply 2 variables so that one of them is in the hour format (hh:mm),
How can I do so? The answer I am getting is 0:
<?php
$num = 5;
$timeUnit = "00:01";
$waitingTime = $num * $timeUnit;
echo $waitingTime;
I need to get 00:05 (5 min) in the output, but I am getting 0.
Hope this will help you
$num=100;
$timeUnit="00:02";
$timeUnit;
$time=explode(":",$timeUnit);
$waitingTime=$num*$time[1];
$minute = sprintf("%02d", ($waitingTime%60));
$hour = sprintf("%02d", ($waitingTime/60));
$current_time = floor($hour).':'.($minute);
echo $current_time;
$num=20;
$timeUnit="00:03";
$time=explode(":",$timeUnit);
$waitingTime=$num*$time[1];
$sec=$time[0]+$waitingTime%60;
$hour=floor($waitingTime/60);
$timeUnit1="".$hour.":".$sec."";
$current=strtotime($timeUnit1); //seconds of current time
echo date("H:i",$current);
You can do like this
I ended up converting the time into seconds, did the multiply and converted it to "time format" again:
$num=200;
$timeUnit="00:01:00";
$time=explode(":",$timeUnit);
$sec=$time[1]*60;
$sec=$sec*$num;
$diff = $sec;
$format = sprintf('%02d:%02d:%02d', ($diff / 3600), ($diff / 60 % 60), $diff % 60);
echo $format;
Thanks for your help
You can't compute string and digits this way.
All you would have to do would be to create a function/method that would implement the behavior you want and use it
I have two times. I want to get time difference between seconds, suppose there are two time time1 = 4:20 and time2 =20:10 now i want to get difference in seconds between them .
i do not have date parameters here ,please do not mark post as duplicate : Getting time difference between two times in PHP
there there is day also , so my case is different
$time1 = strtotime('01:30:00');
$time2 = strtotime('02:20:00');
$time_def = ($time2-$time1)/60;
echo 'Minutes:'.$time_def;
This function will work for MM:SS and HH:MM:SS format:
function TimeToSec($time) {
$sec = 0;
foreach (array_reverse(explode(':', $time)) as $k => $v) $sec += pow(60, $k) * $v;
return $sec;
}
To calculate difference, use:
echo TimeToSec('20:10') - TimeToSec('4:20');
demo
If 4 in 4:20 is minutes and 20 is seconds:
function minutesAndSecondsToSeconds($minutesAndSeconds) {
list($minutes, $seconds) = explode(':', $minutesAndSeconds);
return $minutes * 60 + $seconds;
}
echo minutesAndSecondsToSeconds('20:10') - minutesAndSecondsToSeconds('4:20');
With hours:
function timeToSeconds($time) {
list($hours, $minutes, $seconds) = explode(':', $time);
return $hours * 3600 + $minutes * 60 + $seconds;
}
echo timeToSeconds('3:20:10') - timeToSeconds('1:20:00');
Or simply use strtotime that should works as explained here: Getting time difference between two times in PHP
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);
}
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!
I am having a time duration in seconds. I want show it in HH:mm format. Is there way to do it using php date-time function??
Use gmdate() and set the seconds as param.
Example:
$seconds = 20;
$result = gmdate('H:i', $seconds);
Edit: Ooops... I placed date instead of gmdate... Now I noticed the problem with the timezones with date.
(It's corrected now.)
If you have seconds you could do:
<?php
$sec = 3864;
$h = floor($sec /3600);
$m = floor(($sec - $h *3600) / 60);
$s = $sec % 60;
printf("%02d:%02d:%02d", $h, $m, $s);
?>
output here: http://codepad.org/7ee9Cx03
i found this solution:
function sec_to_his ($seconds)
{
return gmdate ('H:i:s', $seconds);
}
some other variations can be found on this website: http://codeaid.net/php/convert-seconds-to-hours-minutes-and-seconds-(php)
greets stefan.
You could write a function:
<?php
function convertToHoursMinutes($seconds) {
$seconds = intval($seconds);
$hours = ceil($seconds/3600);
$minutes = $seconds%3600;
return sprintf('%d:%02d', $hours, $minutes);
}
echo '24502 seconds is ' . convertToHoursMinutes(24502);
Or you could try searching Google your question.