Round formatted time value to the nearest half-hour - php

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;

Related

Removing seconds from time and rounding the minutes

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

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.

Calculate Number of hours worked in 2 24 hour time format PHP

Hi I have a two variable in 24 hour time format and want to compute the number of hours worked. But I get negative and wrong value
I'm using PHP and here's my code
$endtime = date( 'g:i A', strtotime( $itInfo['endTime'] ) );
$startTime = date( 'g:i A', strtotime( $itInfo['startTime'] ) );
$timeDiff = (strtotime($endtime) - strtotime($startTime))/60/60;
$total = strtotime($endtime) - strtotime($startTime);
$hours = floor($total / 60 / 60);
$minutes = round(($total - ($hours * 60 * 60)) / 60);
echo "FROM ".$itInfo['startTime']." TO ".$itInfo['endTime']." (".$hours.'.'.$minutes."hours)";`
Here's the output FROM 22:00 TO 03:00 (-19.0hours) which is wrong the output should be 5 Hours.
Try this:
$timeDiff = strtotime($itInfo['endTime']) - strtotime($itInfo['startTime']);
echo substr('00'.($timeDiff / 3600 % 24),-2)
.':'. substr('00'.($timeDiff / 60 % 60),-2)
.':'. substr('00'.($timeDiff % 60),-2);
As others have stated, best to work with timestamps. But with your current code, you should be able to add this right before the echo:
if ($itInfo['startTime'] > $itInfo['endTime']) {
$hours = 24 - $hours;
}
This would be: 24 - 19 = 5.
Also, be sure to take add abs() in your $total variable as well:
$total = abs(strtotime($endtime) - strtotime($startTime));

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;

Finding the difference between 2 dates

I made this and needed some help tweaking it so that it gives the proper outcome
function daysDifference($end){
//$start = "2007-03-24";
//$end = "2009-06-26";
$now = date("Y-m-d");
$e = (is_string($end) ? strtotime($end) : $end);
$diff = abs($e - strtotime($now));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24)/ (60 * 60 *24));
return ($years == 0 ? '' : ($years == 1 ? $years . ' year ' : $years . ' years ')) . ($months == 0 ? '' : ($months == 1 ? $months . ' month ' : $months . ' months ')) . ($days == 0 ? '' : ($days == 1 ? $days . ' day ' : $days . ' days '));
}
$end is being pulled from my database so there is checks to see if its a string or a date already.
$e now can be used, but when I tried to subtract $now from $e I get funny results
for instance:
$now being today the 13th and $e being an end date for a project, it's suppose to give me what I need... right?
Where I'm suppose to get say 12 days remaining, I get 1 year 12 days.
and where $e = 0000-00-00 (in case the user didn't input an end date), I get 40 years 10 months and 26 days remaining.
I tried alot of different variations to my calculations but I keep getting nowhere.
Why would you reinvent the wheel? Use date_diff:
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');
?>
Store real dates, not strings, and you can just ask the database for the difference.
SELECT DATEDIFF(CURRENT_DATE, end) FROM table
If you just go dividing things by 365 you won't get accurate results. Not every year has 365 days.

Categories