Removing seconds from time and rounding the minutes - php

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));
}

Related

If hours over 24 don't reset

I currently made a function which tells from timestamp the hours, minutes and seconds left. But when the item is ending in more than 24 hours it should show example 48 hours and not resetting it. How can i do this?
I have days already, but i should be only in hours, minutes and seconds, ex.
76 hours, 20 minutes & 20 seconds.
$time = strtotime($row['start_date']) - time();
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
$hours = floor($time / (60 * 60));
$time -= $hours * (60 * 60);
$minutes = floor($time / 60);
$time -= $minutes * 60;
$seconds = floor($time);
$time -= $seconds;
$hours = ($hours<10) ? "0" . $hours : $hours;
$minutes = ($minutes<10) ? "0" . $minutes : $minutes;
$seconds = ($seconds<10) ? "0" . $seconds : $seconds;
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
Your code removes the day part from the timestamp, that's why.

Negative hours value in DateInterval::format()

i need to convert number of seconds to date, and then show time difference as days, hours and seconds.
But for some reason after some number of days, in last hour before next full day I get negative hours value. Right now this problem occures if date is greater then 38 days (it's before 1st November). Maybe tomorrow this value will be different, I'm not sure.
Code:
$s = 84600; // 23.5 h in seconds
$s += (60*60*24)*38; // add 38 days in seconds
$d = (new \DateTime())->modify("-".$s."seconds");
echo (new \DateTime())->diff($d)->format("%a days %h:%I");
// shows: 38 days -1:30
Same code with 1 day of difference:
$s = 84600; // 23.5 h in seconds
$s += (60*60*24)*37; // add 37 days in seconds
$d = (new \DateTime())->modify("-".$s."seconds");
echo (new \DateTime())->diff($d)->format("%a days %h:%I");
// shows: 37 days 23:30
PHP version 5.6.2. Tested on localhost, server and http://sandbox.onlinephpfunctions.com/ with same result.
Not sure if you looking for this kind of work around
<?php
$s= 84600;
$ts= strtotime('38 day 30 second', 0);
$difference=$ts-$s;
$days = floor($difference / 86400);
$hours = floor(($difference - $days * 86400) / 3600);
$minutes = floor(($difference - $days * 86400 - $hours * 3600) / 60);
$seconds = floor($difference - $days * 86400 - $hours * 3600 - $minutes * 60);
echo "{$days} days {$hours} hours {$minutes} minutes {$seconds} seconds";
?>
//Output 37 days 0 hours 30 minutes 30 seconds

Formatting seconds to a readable format [duplicate]

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.

How to round date to 00 or 30? [duplicate]

I need 0:30 through 0:59 to round down to 0:30.
I need 0:00 through 0:29 to round up to 0:30.
Example: 08:56 will round down to 08:30 where 09:00 and 09:01 will need to round up to 09:30.
Seconds should be omitted or rounded to :00
Why don't you just do this:
$hour = date('H');
$minute = (date('i')>30)?'30':'00';
echo "$hour:$minute";
DEMO
UPDATE
I just realized that you asked all time to be rounded to :30:
$hour = date('H');
$minute = '30'; //always 30
echo "$hour:$minute";
My solution to a real round are the next options:
//Round Down
date('H:i',floor(time() / (15 * 60)) * (15 * 60))
//Round Up/Down
date('H:i',round(time() / (15 * 60)) * (15 * 60))
//Round Up
date('H:i',ceil(time() / (15 * 60)) * (15 * 60))
Replace time() with the time you need, and 15 with the minutes you like to round near to.
Example:
$currentTime = time();
echo('Current Time: ' . date('H:i',$currentTime) . '<br>Rounded Up 15 Minutes time: ' . date('H:i',ceil($currentTime / (15 * 60)) * (15 * 60)));
you have to do:
$hour = date('H');
$minute = date('i');
$minute = ($minute < 30 || $minute > 30) ? 30 : $minute;

Round formatted time value to the nearest half-hour

I need 0:30 through 0:59 to round down to 0:30.
I need 0:00 through 0:29 to round up to 0:30.
Example: 08:56 will round down to 08:30 where 09:00 and 09:01 will need to round up to 09:30.
Seconds should be omitted or rounded to :00
Why don't you just do this:
$hour = date('H');
$minute = (date('i')>30)?'30':'00';
echo "$hour:$minute";
DEMO
UPDATE
I just realized that you asked all time to be rounded to :30:
$hour = date('H');
$minute = '30'; //always 30
echo "$hour:$minute";
My solution to a real round are the next options:
//Round Down
date('H:i',floor(time() / (15 * 60)) * (15 * 60))
//Round Up/Down
date('H:i',round(time() / (15 * 60)) * (15 * 60))
//Round Up
date('H:i',ceil(time() / (15 * 60)) * (15 * 60))
Replace time() with the time you need, and 15 with the minutes you like to round near to.
Example:
$currentTime = time();
echo('Current Time: ' . date('H:i',$currentTime) . '<br>Rounded Up 15 Minutes time: ' . date('H:i',ceil($currentTime / (15 * 60)) * (15 * 60)));
you have to do:
$hour = date('H');
$minute = date('i');
$minute = ($minute < 30 || $minute > 30) ? 30 : $minute;

Categories