This question already has answers here:
Convert seconds into days, hours, minutes and seconds
(29 answers)
Closed 9 years ago.
I know about the date and gmdate functions, but my needs are different in this case.
I have a number of seconds which I need to convert to something like:
A days B hours C minutes D seconds
I don't know how to format my seconds like this.
Any help would be appreciated.
Just use floor and the values 86400 (seconds in a day - 60 * 60 * 24), 3600 (seconds in an hour - 60 * 60) and 60 (seconds in a minute):
<?php
function secondsToTime($seconds) {
$days = floor($seconds / 86400);
$seconds -= ($days * 86400);
$hours = floor($seconds / 3600);
$seconds -= ($hours * 3600);
$minutes = floor($seconds / 60);
$seconds -= ($minutes * 60);
$values = array(
'day' => $days,
'hour' => $hours,
'minute' => $minutes,
'second' => $seconds
);
$parts = array();
foreach ($values as $text => $value) {
if ($value > 0) {
$parts[] = $value . ' ' . $text . ($value > 1 ? 's' : '');
}
}
return implode(' ', $parts);
}
var_dump(secondsToTime(1234561));
//string(36) "14 days 6 hours 56 minutes 1 second"
?>
DEMO
This can be achieved with Date and Time extension:
Use example:
echo secondsToTime(1234563);
# 14 days, 6 hours, 56 minutes and 3 seconds
Link to the function + Demo.
Related
If I add 1 hour and 35 minutes in PHP and subtract 4 hours, I get a remaining time of -3 hours and 30 minutes.
But correct would be -2 hours and 30 minutes. Where’s the mistake?
$minutes = null;
while ($z = $stmt->fetch()) {
$minutes += $z['minutes'];
}
if ($stmt = $pdo->prepare($sql)) {
$stmt->execute(array(
':user' => $user
));
$negativeTime = null;
while ($z = $stmt->fetch()) {
$negativeTime += $z['minutes'];
}
}
$minutes = $minutes - $negativeTime;
echo (floor($minutes / 60)) . " hours and " . ($minutes - floor($minutes / 60) * 60) . " minutes";
$minutes - floor($minutes/60) is positive. So what you're getting is -3 hours plus 30 minutes, which is -2:30.
You need to treat positive and negative values of $minutes differently, so that both the hours and minutes are the distance from zero. Use the abs() function to convert the time to positive, perform the calculation, then show the hours as negative if the original value of $minutes was negative.
$sign = $minutes < 0 ? '-' : '';
$minutes = abs($minutes);
$hours = floor($minutes / 60);
$minutes = $minutes % 60;
echo "$sign$hours hours and $minutes minutes";
DEMO
I'm storing time entries to a variable called duration. So right now If I log a time entry, it will be in the standard format: ex) 12:30:00
What I want to do is remove the seconds part of the time entry and round the minutes to every 15th minute. Also I'de like to remove the 0 in the front of if the times before 10.
So 09:00:00 would become 9
09:30:00 would be come 9:30
So 12:30:00 would be 12:30.
12:08:00 would be 12:15
12:34:00 would be 12:30 ect ect.
Here's the code I was using:
$duration = '';
if ($seconds < 0) {
$duration = '-';
$seconds = abs($seconds);
}
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
return $duration . sprintf('%d:%02d:%02d', $hours, $minutes, $seconds);
}
and here's a sample output:
1 => "12:20:00"
Try this.
$currentTime = strtotime('12:34:00');
echo 'Rounded Up 15 Minutes time: ' . date('H:i',round($currentTime / (15 * 60)) * (15 * 60));
//output - Rounded Up 15 Minutes time: 12:30
If this
$currentTime = strtotime('12:08:00');
echo 'Rounded Up 15 Minutes time: ' . date('H:i',round($currentTime / (15 * 60)) * (15 * 60));
//output - Rounded Up 15 Minutes time: 12:15
check demo. demo
For round, you may check with minutes zero condition like.
$currentTime = strtotime('9:00:00');
if(date('i',ceil($currentTime / (15 * 60)) * (15 * 60)) == 00){
echo date('H',ceil($currentTime / (15 * 60)) * (15 * 60));
}
I have the following code:
echo $diff . ' / ';
echo gmdate("H:i:s", $diff);
This produces
129600 / 12:00:00
However 129600 is 36 hours and not 12, so how could I amend the code to be total hours (36) rather than being 1 day and 12 hours as I don't need to show the day
So if it was 36 hours and 1 minute, I'd want to show: 36:01 or 36:1
Thanks
$hours = floor($diff/ 3600);
$minutes = floor(($diff/ 60) % 60);
$seconds = $diff% 60;
$diff= $hours . ":" . $minutes . ":" . $seconds ;
echo $diff ;
Try this. This may give you the desired result.
Fetch the days, multiply it by 24 - then add the hours to that, and append the minutes and seconds.
Note that if you have more than 30/31 days, this will stop working again and you will need to account for months or possibly years as well.
$days = gmdate("d", $diff);
$hours = gmdate("H", $diff);
echo ($days*24 + $hours).gmdate(":i:s", $diff);
You can instead go the other way around - extract the seconds and minutes!
$seconds = str_pad($diff % 60, 2, '0', STR_PAD_LEFT);
$minutes = str_pad($diff/60 % 60, 2, '0', STR_PAD_LEFT);
$hours = str_pad($diff/3600, 2, '0', STR_PAD_LEFT);
echo "$hours:$minutes:$seconds";
Live demo at https://3v4l.org/Aa8E6
As of your comment : This doesn't work if it's 08-05-2019 12:00 AM and 09-05-2019 12:00 PM - it brings back 60.
Try following code
$sstartdate = new DateTime('08-05-2019 12:00:00 AM');
$edatetime = new DateTime('09-05-2019 12:00:00 PM');
$since_start = $sstartdate->diff($edatetime);
$hours = ($since_start->days * 24)+($since_start->h);
$min = $since_start->i;
$sec = $since_start->s;
echo $hours.':'.$min;
Output: 36:00
36 hours and 00 minute
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
How may I turn days from the gmdate(); into hours?
gmdate("d H:i:s", '115932'); // 02 08:12:12
What I want to return is 56:12:12
gmdate("H:i:s", '115932'); // 08:12:12
EDIT: To clarify, I just want to convert a numeric string into the H:i:s format.
To convert days d to hours, just multiply by by 24. Then add hours H.
Try this,
$date = '115932';
$minsec = gmdate("i:s", $date);
$hours = gmdate("d", $date)*24 + gmdate("H", $date);
$time = $hours . ':' . $minsec; // 56:12:12
See demo
Extract the hours, minutes and seconds, like this:
$time = 115932;
$mins = $time % 3600;
$secs = $mins % 60;
$hours = ($time - $mins) / 3600;
$mins = ($mins - $secs) / 60;
echo $hours . ':' . (($mins < 10 ) ? '0' : '') . $mins . ':' . (($secs < 10 ) ? '0' : '') . $secs;
// Or like this
vprintf('%02d:%02d:%02d',$hours,$mins,$secs);
i have been having trouble displaying the hours and minutes after calculating the difference between 2 different unix timestamps. Lets say i have these 2 unix timestamps:
1) 1384327800
2) 1384300800
the difference is
27000
if i divide 27000/3600 i get
7.5
but what i want is to display
7:30
or
7 hours and 30 minutes
what is the best way to go about this?
All you need to do it a little calculation:
$diff = 27000;
$hour = floor($diff / 3600);
$min = floor(($diff - $hour * 3600) / 60);
$sec = $diff - $hour * 3600 - $min * 60;
echo "$hour hours, $min minutes, $sec seconds";
Or try it with DateTime class:
$dt1 = new DateTime('#1384327800');
$dt2 = new DateTime('#1384300801');
$diff = $dt1->diff($dt2);
echo $diff->format('%h hours, %i minutes, %s seconds');
Algorithmic way (with no automatic calculation with any function/library) :
$diff_in_minutes = ($timestamp1 - $timestamp2) / 60;
$minutes = $diff_in_minutes % 60;
$hours = ($diff_in_minutes - $minutes) / 60;
$diff_string = $hours . ':' . $minutes;
Look at DateTime() and DateInterval::format()
$dt1 = new DateTime('#1384327800');
$dt2 = new DateTime('#1384300800');
$diff = $dt1->diff($dt2);
echo $diff->format('%h hours and %i minutes');
This last little bit will remove unnecessary time periods from your string if so desired.
$elapsed = $diff->format('%y years, %m months, %a days, %h hours, %i minutes, %S seconds');
$elapsed = str_replace(array('0 years,', ' 0 months,', ' 0 days,', ' 0 hours,', ' 0 minutes,'), '', $elapsed);
$elapsed = str_replace(array('1 years, ', ' 1 months, ', ' 1 days, ', ' 1 hours, ', ' 1 minutes'), array('1 year, ', '1 month, ', ' 1 day, ', ' 1 hour, ', ' 1 minute'), $elapsed);
echo $elapsed;
For full long formatting exactly as described:
date_default_timezone_set('UTC');
$timestamp1 = new DateTime('#1384327800');
$timestamp2 = new DateTime('#1384300800');
$diff = $timestamp1->diff($timestamp2);
$timemap = array('y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second');
$timefmt = array();
foreach ($timemap as $prop => $desc) {
if ($diff->$prop > 0) {
$timefmt[] = ($diff->$prop > 1) ? "{$diff->$prop} {$desc}s" : "{$diff->$prop} $desc";
}
}
$diffstr = (count($timefmt) > 1)
? $diff->format(sprintf('%s and %s',
implode(', ', array_slice($timefmt, 0, -1)), end($timefmt)))
: end($timefmt);
var_dump($diffstr);
This gave me the following:
string(22) "7 hours and 30 minutes"