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
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'm using the below calculator to determine the years, months, and days between a set date and the current date. I thought it was working fine, but then it came up on the year mark and i noticed it was not working properly. Tomorrow is actually when the one year mark would be, but it is currently returning 11 months, 34 days. Could anyone tell me whats wrong? It should be 11 months, 30 days.
function relationshipTimer($functionDate)
{
$date1 = $functionDate;
$date2 = date("Y-m-d");
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - ($years * 365 * 60 * 60 * 24)) / ((365 * 60 * 60 * 24) / 12));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
if ($years > 0) {echo $years . " Year";}
if ($years > 1) {echo "s ";}
if ($months > 0) {echo " " . $months . " Month";}
if ($months > 1) {echo "s ";}
if ($date1 == $date2) {echo "1 Day ";}
if ($days > 0) {echo $days . " Day";}
if ($days > 1) {echo "s ";}
}
And this is where $functionDate comes from:
relationshipTimer("2018-04-28");
When you calculate $days, you are assuming that all months are 30 days long, which is obviously wrong. Therefore you get a year that is 11 months plus 35 days (36 days for leap years).
Processing dates is complicated. You should always use specialized tools like PHP's DateTime::diff()
For example, with:
$date1 = new DateTime("2018-04-28");
$date2 = new DateTime("2019-04-27");
$diff = $date2->diff($date1);
print $diff->format("%y years %m months %d days\n");
... you get (because April has 30 days):
0 years 11 months 29 days
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));
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;
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.