Get formated time for a video duration in php - php

This should be easy...
I have a duration in seconds,
I want to output it in hours:minutes:seconds format
When I try...
// video duration is 1560 seconds
<?=date("h:i:s",$video->duration)?>
...it outputs 07:26:00 instead of 00:26:00. So I figure it's a time zone issue.
At the beginning of my application I set the timezone like this
date_default_timezone_set('America/Montreal');
Now I guess I would get my normal value if I change the timezone to GMT but I don't want to do that each time I want a simple duration (and re-set the timezone to America/Montreal)
What are my options?

date is used to get a human readable format for an unix timestamp, so it's not what you need. In other words that function is used to show a date (like 12.12.2012) and not the duration of your video
for that you can create a function that starts from the total number of seconds and does something like
$seconds = $total_time %60;
$minutes = (floor($total_time/60)) % 60;
$hours = floor($total_time/3600);
return $hours . ':' . $minutes . ':' . $seconds;
maybe you need to adjust this a bit, as I did not test it. also some tests to skip hours or minutes if the video is short

This is an old question but I thought I can provide an answer for this:
function convert_to_duration($total_time){
$seconds = $total_time %60;
$minutes = (floor($total_time/60)) % 60;
$hours = floor($total_time/3600);
if($hours == 0){
return $minutes . ':' . sprintf('%02d', $seconds);
}else{
return $hours . ':' . $minutes . ':' . sprintf('%02d', $seconds);
}
}

Related

PHP - How to deduct a time to a total time without converting to decimal?

I'm currently working with a timekeeping system which computes the sum of the basic hours of the week and deduct certain time if there's a late record.
Given that the employee has a total hours rendered for this week is 45 hours (45:00), and he she/has a total late record for that week of 50 minutes (00:50),
Using, PHP. How can I deduct the late record to the total hours rendered without converting time to decimal? The desired output for the above sample is 44:10 since 00:50 is deducted to 45:00.
I see so your goal is to subtract durations ex.
45:00 - 00:50 = 44:10
1: Create a function that convert them into hours
function convertToHours($duration) {
$duration = explode(':',$duration);
$hours+= (int)$duration[0];
$hours+= (int)$duration[1] / 60;
return $hours;
}
2: Create a funciton thats convert from seconds to duration hours:seconds
function secondsToDuration($seconds) {
$H = floor($seconds / 3600);
$i = ($seconds / 60) % 60;
$s = $seconds % 60;
return sprintf("%02d:%02d:%02d", $H, $i, $s);
}
Convert them into hours using function created
$duration1 = convertToHours("25:00");
$duration2 = convertToHours("00:50");
Then subtract them
$difference = $duration1 - $duration2;
Lastly use the created method which convert them back into duration
$duration = secondsToDuration($difference * 3600);
See Demo here
Hope it helps you
You can convert the string to a date and get the difference.
$d1 = "00:45:00";
$d2 = "00:00:50";
date_default_timezone_set("utc");
$fakedate = '01/01/2017';
$d1 = $fakedate . ' ' . $d1;
$d2 = $fakedate . ' ' . $d2;
$dt1 = new DateTime($d1);
$dt2 = new DateTime($d2);
$diff = $dt1->diff($dt2);
echo $diff->format("%H:%I:%S");
The output will be: 00:44:10

date format in PHP date function [duplicate]

This question already has answers here:
Calculating the difference between two times using php, and then adding it to a total of time type
(2 answers)
Closed 9 years ago.
Hi I have values stored in MSSQL database as time(7)
job_start
job_end
I am trying to work out the difference between these two times using php, i have the folowing code
$start = $model->job_start;
$end = $model->job_end;
$diff = date( "h:i:s", strtotime($end) - strtotime($start) ) ;
echo $diff ;
However the output is
03:06:00
Rather than
00:06:00
By the way the difference is 6 minutes (360 seconds), can anyone help get this in the right format so i can save it ?
This question has been marked as a duplicate, however it is a different question as this addresses a specific timezone question when using the date function. And has been answered as such.
EDIT
The code i have marked answered the question i asked however I am trying to add an extra calculation as follows
$start = $model->job_start;
$end = $model->job_end;
$total = $model->customer->total_time;
//adding time difference to total time used for that customer
$dt = strtotime($total) + strtotime($end) - strtotime($start);
$hours = floor($dt / 3600);
$minutes = floor($dt / 60) - $hours * 60;
$seconds = $dt - $hours * 3600 - $minutes * 60;
// Padded values
$hours = str_pad($hours, 2, STR_PAD_LEFT, '0');
$minutes = str_pad($minutes, 2, STR_PAD_LEFT, '0');
$seconds = str_pad($seconds, 2, STR_PAD_LEFT, '0');
$output = "{$hours}:{$minutes}:{$seconds}" ;
//display
echo '<br> Start time : '. $start;
echo '<br> End time : '. $end;
echo '<br> total time + time difference : '. $output;
echo '<br> Total time for Customer : '. CHtml::encode($model->customer->total_time);
I am trying to add the difference between the total times to a total time variable
However when output is echoed i get this display
Start time : 11:45:00.0000000
End time : 12:45:00.0000000
total time + time difference : 382015:00:00
Total time for Customer : 09:00:00.0000000
Note that the total time + time difference has extra digits before it , its probably a simple mistake on my part but I can't see it , any help would be appreciated
Why not do this in the database
SELECT TIMESTAMPDIFF(SECOND, job_start, job_end);
Its faster and it is clearer for others to see what you are trying to do.
First of all, date gives date since Unix epoch (1970-01-01 00:00:00) so the odd three hour difference you see there is your timezone. You need to parse the hours, minutes and seconds separately.
<?php
$end = '2013-07-30 23:27:00';
$start = '2013-07-30 21:23:56';
$dt = strtotime($end) - strtotime($start);
$hours = floor($dt / 3600);
$minutes = floor($dt / 60) - $hours * 60;
$seconds = $dt - $hours * 3600 - $minutes * 60;
// Padded values
$hours = str_pad($hours, 2, STR_PAD_LEFT, '0');
$minutes = str_pad($minutes, 2, STR_PAD_LEFT, '0');
$seconds = str_pad($seconds, 2, STR_PAD_LEFT, '0');
$output = "{$hours}:{$minutes}:{$seconds}";

PHP Countdown 48 hours from a set datetime

I'm looking to create a countdown timer in PHP. When a user clicks a button it saves the current date & time into a database entry, then it should take the difference of that entry with the current date and time and 'doSomething' when the difference is larger than 48 hours.
My issue is with the actual countdown.
I've tried the following but to no avail it only counts the difference of the of both strings and doesn't take the days in account. Not only that but it also appears to show the resulted difference incorrectly:
$d1=strtotime("2012-07-08 11:14:15");
$d2=strtotime("2012-07-09 12:14:15");
$diff = round(abs($d1 - $d2));
$cd = date("H:i:s", $diff);
echo $cd;
Thanks for helping me Yan.kun from StackOverflow! The code submitted below was the solution! In order to display strictly the countdown in hours:minutes:seconds I replaced the printf() code with the following:
$hours = ($result->d*24)+$result->h;
$minutes = $result->i;
$seconds = $result->s;
echo $hours . ":" . $minutes . ":" . $seconds;
Try this instead:
$d1 = new DateTime("2012-07-08 11:14:15");
$d2 = new DateTime("2012-07-09 12:14:15");
$result = $d1->diff($d2);
printf('difference is %d day(s), %d hour(s), %d minute(s)', $result->d, $result->h, $result->i);
EDIT:
And if you have no PHP 5.3 available, you can convert your times into an unix epoch timestamp like described in this answer.
not sure if this is what you want:
$d1=strtotime("2012-11-18 11:14:15");//2 days earlier
$d2=strtotime("2012-11-20 11:14:15");//today
$diff = $d2 - $d1; //difference in seconds.
$hours = $diff/60/60;//translation to minutes then to hours
echo $hours;
if($hours>48){
echo "Script";
}

Converting seconds to hours/minutes/seconds in PHP [duplicate]

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!

form value 00:00:00 into time value

I was wondering, if I record a time through a javascript function and say I leave it on for 2 mins 20 seconds:
00.02.20
now when I insert this into my database, where the field type is set as time, it doesn't record properly.
I think it comes from how I request the data:
$length = mysql_escape_string($_REQUEST['timeDrive']);
is there a way of converting this into a time that is going to output the exact value:
00.02.20
thanks for the help!
If you don't need to perform any mathematical functions on those fields (eg: sum, average, etc), and it's purely for display, then you can just store it as plain text in a CHAR(8) field.
Otherwise, you'll need to normalise it to some sort of unit. I'd suggest maybe as an integer of the total number of seconds (if that is accurate enough for you).
To convert to seconds:
$val = "00.02.20";
$parts = explode(".", $val); // ['00', '02', '20']
$totalSeconds = parts[0] * 3600 + parts[1] * 60 + parts[2]; // 140
And to convert it back:
$seconds = 140; // this would be read from the database, or something
printf("%02d.%02d.%02d",
$seconds / 3600,
($seconds / 60) % 60,
$seconds % 60
);
Nope, not just like this.
It would be possible, if you make it just more simple and you just count the number of seconds in javascript. In your Database you can save it as integer or something.
The other way is you save it just as string, but then you can't compare the times.
Well, I'd save it as an integer, and if you want to show the time to somebody, you would have to format it before, like this:
function formatTime($seconds) {
$hours = floor($seconds / 3600); //by nickf
$minutes = floor($seconds / 60) % 60;
$sec = $seconds % 60;
$retHr = (strlen($hours) == 1 ? "0" . $hours : $minutes);
$retMin = (strlen($minutes) == 1 ? "0" . $minutes : $minutes);
$retsec = (strlen($sec) == 1 ? "0" . $sec : $sec);
return $retHr . "." . $retMin . "." . $retsec;
}
//now you counted one hour, two minutes and three seconds, you call it like
echo formatTime(3723);
//... and it outputs "01.02.03"

Categories