I have converted the length of the movie Avatar from 2009, from minutes to timestamp. The movie is 162 minutes long so the timestamp is 1386227800. Now I need to convert the timestamp to hours and minutes which I don't know how.
In short: how can I convert a timestamp and get the correct result in hours and minutes?
I have tested floor(1386227800 / 60), date('H:i', mktime(0, 1386227800) and some functions that converts a timestamp to hours and minutes, but these only converts the hours to something endless, like 12375 or something like that.
So, how can I accomplish this?
As one of the commenters mentioned, a timestamp represents a single point in time, not a duration. There's no reason to call strtotime at all -- if you already have the total minutes, you can ignore converting it to a timestamp and just get that into hours:minutes like this:
$time=162;
$hours = floor($time / 60);
$minutes = ($time % 60);
echo $hours.":".$minutes;
echo date('H:i:s', 1386227800);
Can you try,
echo date('H:i','1386227800');
As I understand it, you want to convert 162 minutes to be represented with hours and minutes. This is a simple case of mathematics.
<?php
$minutes = 162 % 60;
$hours = floor(162 / 60);
?>
$hours will return 2, and $minutes will return 42. 2 hours, 42 minutes is equivalent to 162 minutes.
Hope this helps.
Related
I'm totalling up time like so.
$totalTimeDiff = new DateTime("#0");
foreach($dbrecords as $row)
{
$timeDiff = date_diff( ... two datetimes from my database ... )
$totalTimeDiff->add($timeDiff);
}
So $totalTimeDiff is a DateTime object with the sum of all of the time differences added together (so a sum of all of the durations). How can I get the total time in seconds?
Why not keep it simple?
$totalseconds=0;
foreach($dbrecords as $row)
$totalseconds+=(UNIX_TIMESTAMP(second_datetime)-UNIX_TIMESTAMP(first_datetime));
use strtotime function
echo strtotime('01:00:00') - strtotime('TODAY');
$totalTimeDiff->format('U');
Taking moonwave99's advice, I used DateInterval (can't remember why I went with DateTime for that in the first place, possibly a workaround for something at another stage of the project) and computed the seconds by adding each value to the total after converting it to seconds (converting hours and minutes to seconds and summing them up). I did this by using the DateInterval class's seconds property as well as the following function to convert a DateInterval to seconds (Note: only accounted for days, hours, minutes, and seconds for my specific case as there's no chance the amount will exceed one month):
function convertDateIntervalToSeconds($dateInterval)
{
$days = $dateInterval->d * 24 * 60 * 60;
$hours = $dateInterval->h * 60 * 60;
$minutes = $dateInterval->i * 60;
$seconds = $dateInterval->s;
return $hours + $minutes + $seconds;
}
I have a time calculations problem in getting average.
I have this summed up call time 06:03:05 and I want to get an average with 175 calls.
date_default_timezone_set('America/Chicago');
$ts = strtotime("06:03:05");
echo date("H:i:s", $ts/175);
I get:
12:26:25
I'm not even sure why I come up with this very huge time average. Am I doing this right? Please help.
The problem there is that your strtotime call, not having a date component, is defaulting to the current date. So the $ts is a much much larger number than just the sum of the time parts; it includes the date parts as well.
I would avoid using the time functions like that. It's simple enough to calculate the number of seconds based on the hours, minutes and seconds. Once you have that, you can use date() to echo the formatted time like you do there.
Try something more like this:
function getTimeAverage($hours, $minutes, $seconds, $division) {
$seconds += ($hours * 3600) + ($minutes * 60);
return $seconds / $division;
}
$average = getTimeAverage(6, 3, 5, 175);
echo gmdate("H:i:s", $average);
One easy way which only works up to 24 hours. Else take Atlis approach and search also for a way to convert seconds to date.
$time = '06:03:05';
$seconds = strtotime("1970-01-01 $time UTC");
$average = $seconds/175;
echo gmdate('H:i:s', $average);
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
I am trying to substract 24 hours time format and then convert it into minutes but it does not work well.
Here is my code
$time1 = '2010-08-05 23:00:00';
$time2 = '2010-08-05 00:00:00';
echo round( (strtotime($time2) - strtotime($time1)) / 60);
it will display this -1380.
if you put 1-23 hour in time2 it will work. I tried to convert time in 12 hours but it didn't work.
Any help would be greatly appreciated.
The result is in minutes not in hours.
strtotime() returns a result in seconds (in the Unix timestamp format). You will need to divide by 3600 to convert to hours.
Try this:
$time1 = '2010-08-05 23:00:00';
$time2 = '2010-08-05 00:00:00';
echo abs ( round( (strtotime($time2) - strtotime($time1)) / 3600) );
The abs() function returns an absolute value. In this case, the final result is 23 hours.
I think this is what you are trying to get:
echo round( (strtotime($time1) - strtotime($time2)) / 3600);
Firstly you were subtracting the times the wrong way round, then you were only dividing by 60 which was giving you your answer in minutes.
I need the unix timestamp - the timestamp i have. Then display the time between like on twitter.
If you have the difference called diff:
$seconds = intval($diff) % 60;
$minutes = intval($diff/60) % 60;
$hours = intval($diff/3600) % 24;
$days = intval($diff/(3600*24));
Is this what you want ?
Not sure what language you need it, but if it will end up in a web page, you may try timeago.
Use example :
echo time_elapsed_string('#1367367755');
echo time_elapsed_string('#1367367755', true);
Output :
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Link to the function.