Returning Time Components with Modulus - time

Someone does 20 Hours 42 Minutes & 16 Seconds in one shift totaling 74536 seconds.
How do I get the hours from number of seconds the person has done for that shift?
20 * 60 * 60 = 72000
42 * 60 = 2520
16 = 16
+ -----
Total = 74536
____________________________
Total % 60 = Seconds (16)
Total % ? = Minutes (42)
Total % ? = Hours (20)
Tried 84600 already; turns out when a number is lower the modulus, it really is not very helpful, and something I am going to have to catch should someone only sign in for a few seconds ...

You need to use both modulus and division:
t = seconds_in_shift;
secs = t % 60;
t /= 60;
mins = t % 60;
t /= 60;
hour = t;
Or:
secs = ttime % 60;
mins = (ttime / 60) % 60;
hour = ttime / 3600;
One other option uses the div() function from Standard C (<stdlib.h>):
div_t v1 = div(ttime, 60);
div_t v2 = div(v1.quot, 60);
After that, v1.rem contains the seconds; v2.rem contains the minutes, and v2.quot contains the hours based on the number of seconds originally in ttime.

Based on Jonathan's reply, I believe the accurate answer should be this…
$total_time = 61000;
$sec = $total_time % 60;
$total_time = ($total_time - $sec) / 60;
$min = $total_time % 60;
$hour = ($total_time - $min) / 60;
echo "$hour hours, $min minutes and $sec seconds";
But if your server has PHP7 installed, you can use the intdiv() function. An integer division in which the remainder is discarded.
// $hour = ($total_time - $min) / 60; old code but still works
$hour = intdiv($total_time, 60); // PHP7 and above code

Related

Countdown inside laravel blade

Hi guys, I'm trying to look for a way on how to make the Time Left column count down. I was guessing on doing it via ajax and updating it every minute but I guess it will affect the performance. What is the best way to achieve this? I need it to show the real-time time left of the column also updating the values in the database. Thank you!
You do not to update the time left everytime in the database, I don't think you need to even store it. You need to store the deadline (The End Date) and then the count down will be only on the client side using JavaScript as follows:
let timer = function (date) {
let timer = Math.round(new Date(date).getTime()/1000) - Math.round(new Date().getTime()/1000);
let minutes, seconds;
setInterval(function () {
if (--timer < 0) {
timer = 0;
}
days = parseInt(timer / 60 / 60 / 24, 10);
hours = parseInt((timer / 60 / 60) % 24, 10);
minutes = parseInt((timer / 60) % 60, 10);
seconds = parseInt(timer % 60, 10);
days = days < 10 ? "0" + days : days;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
document.getElementById('cd-days').innerHTML = days;
document.getElementById('cd-hours').innerHTML = hours;
document.getElementById('cd-minutes').innerHTML = minutes;
document.getElementById('cd-seconds').innerHTML = seconds;
}, 1000);
}
//using the function
const today = new Date()
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
timer(tomorrow);
<span id="cd-days">00</span> Days
<span id="cd-hours">00</span> Hours
<span id="cd-minutes">00</span> Minutes
<span id="cd-seconds">00</span> Seconds

work out how many seconds have expired in total during game play

~Why the hell has this had down votes.... you people are weird!
Ok so this is a very simply HTML5 and jQuery and PHP game. Sorry to the people who have answered, I forgot to say this is a php script, i have updated here to reflect.
the first level takes 1 minute. Every level after that takes an extra 10 seconds than the last level. like so;
level 1 = 60 seconds
level 2 = 70 seconds
level 3 = 80 seconds
level 4 = 90 seconds
and so on infinitely.
I need an equation that can figure out what is the total amount of seconds played based on the users level.
level = n
i started with (n * 10) + (n * 60) but soon realized that that doesn't account for the last level already being 10 seconds longer than the last. I have temporarily fixed it using a function calling a foreach loop stopping at the level number and returning the value. but i really want an actual equation.
SO i know you wont let me down :-)
Thanks in advance.
this is what i am using;
function getnumberofsecondsfromlevel($level){
$lastlevelseconds = 60;
while($counter < $level){
$totalseconds = $lastlevelseconds+$totalseconds;
$lastlevelseconds = $lastlevelseconds + 10;
$counter++;
}
return $totalseconds;
}
$level = $_SESSION['**hidden**']['thelevel'];
$totaldureationinseconds = getnumberofsecondsfromlevel($level);
but i want to replace with an actual equation
like so;(of course this is wrong, this is just the example of the format i want it in i.e an equation)
$n = $_SESSION['**hidden**']['thelevel']; (level to get total value of
in seconds)
$s = 60; (start level)
$totaldureationinseconds = ($n * 10) + ($s * $n);
SOLVED by Gopalkrishna Narayan Prabhu :-)
$totalseconds = 60 * $level + 5* (($level-1) * $level);
var total_secs = 0;
for(var i = 1; i<= n ;i++){
total_secs = total_secs + (i*10) + 50;
}
for n= 1, total_secs = 0 + 10 + 50 = 60
for n= 2, total_secs = 60 + 20 + 50 = 130
and so on...
For a single equation:
var n = level_number;
total_secs = 60 * n + 5* ((n-1) * n);
Hope this helps.
It seems as though you're justing looking for the equation
60 + ((levelN - 1) * 10)
Where levelN is the current level, starting at 1. If you make the first level 0, you can get rid of the - 1 part and make it just
60 + (levelN * 10)
Thought process:
What's the base/first number? What's the lowest it can ever be? 60. That means your equation will start with
60 + ...
Every time you increase the level, you add 10, so at some point you'll need something like levelN * 10. Then, it's just some fiddling. In those case, since you don't add any on the first left, and the first level is level 1, you just need to subtract 1 from the level number to fix that.
You can solve this with a really simple mathematical phrase (with factorial).
((n-1)! * 10) + (60 * n)
n is the level ofcourse.

Round minutes 20 in 20 in visual fox pro

please i'm having problems resolving a function about a program in Visual Fox Pro.
I need to make a rounding down every 20 minutes in decimal.
For example: if i recive 19 minutes (0.316) y need return 0 minutes.
if i get between 0-19 minutes, return 0 minutes
if i get between 20-39 minutes, return 20 minutes
if i get between 40-59 minutes, return 40 minutes
if i get between 60-79 minutes, return 60 minutes
i was thinking use ROUND() but i don't know how because "Round" Approaches to the nearest decimal.
thanks in advance.
roundedMinutes = Floor( m.myMinute / 20 ) * 20
For example:
Create Cursor SampleMin (minutes i, rounded i)
Local ix
For ix = 1 To 600
Insert Into SampleMin ;
(minutes, rounded) ;
values ;
(m.ix, Floor(m.ix/20) * 20)
Endfor
Locate
Browse
Here's a small prg I made that will hopefully help you or at least start you in the right direction.
lnStart = 0
lnEnd = 20
lnReceivedMinutes = 500
llNotDone = .T.
IF lnReceivedMinutes > 0
DO WHILE llNotDone
IF BETWEEN(lnReceivedMinutes, lnStart, lnEnd)
llNotDone = .F.
MESSAGEBOX(ALLTRIM(STR(lnStart)) + " Minutes")
ELSE
lnStart = lnStart + 20
lnEnd = lnEnd + 20
ENDIF
ENDDO
ENDIF
I check to see if the value received is between my lnStart and lnEnd. If it is not then I check for the next 20 minutes.
Cetin Basoz gave correct answer
roundedMinutes = Floor( m.myMinute / 20 ) * 20
I have checked myself

How to convert minutes to days, hours, and minutes?

I am a beginner to Python so I do not know a lot of terms or anything really. Can I ask on how to convert minutes to hours and minutes
EX: 75 minutes ->0 days, 1 hour, 15 minutes
print("Welcome to the Scheduler!")
print("What is your name?")
name = input()
print("How many chocolates are there in the order?")
chocolates = input()
print("How many chocolate cakes are there in the order?")
chocolate_cakes = input()
print("How many chocolate ice creams are in the order?")
chocolate_ice_creams = input()
total_time = float(chocolates) + float(chocolate_cakes) + float(chocolate_ice_creams)
print("Total Time:")
print("How many minutes do you have before the order is due?")
minutes = input()
extra_time = float(minutes) - float(total_time)
print("Your extra time for this order is", extra_time)
time = extra_time // 60
print("Thank you,", name)
Well if you're given an input in minutes that is greater than equal to 1440 minutes, then you have at least a day. So to handle this (and the other aspects of time) we can use modulus (%).
days = 0
hours = 0
mins = 0
time = given_number_of_minutes
days = time / 1440
leftover_minutes = time % 1440
hours = leftover_minutes / 60
mins = time - (days*1440) - (hours*60)
print(str(days) + " days, " + str(hours) + " hours, " + str(mins) + " mins. ")
This should work.
# Python Program to Convert seconds
# into hours, minutes and seconds
def convert(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%02d" % (hour, minutes, seconds)
# Driver program
n = 12345
print(convert(n))
method = a
import datetime
str(datetime.timedelta(seconds=666))
'0:11:06'
method = b
def convert(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%02d" % (hour, minutes, seconds)
from datetime import datetime
day = minutes = hours = 0
day = datetime.now ()
day = day.day
minutes = datetime.now ()
minutes = minutes.minute
hours = datetime.now ()
hours = hours.hour
print ("It's day" + str (day) + "and it's" + str (minutes) + "minutes and" + str (hours) + "hours.")
This code above does not work I started to make my own version of how this code could help you remember I am an amateur is true
from datetime import datetime
day = minutes = hours = 0
time = datetime.now().minute
days = time / 1440
leftover_minutes = time % 1440
hours = leftover_minutes / 60
mins = time - (days*1440) - (hours*60)
print(str(days) + " days, " + str(hours) + " hours, " + str(mins) + " mins. ")
You actually have to round down values in order to get integers.
import math
def transform_minutes(total_minutes):
days = math.floor(total_minutes / (24*60))
leftover_minutes = total_minutes % (24*60)
hours = math.floor(leftover_minutes / 60)
mins = total_minutes - (days*1440) - (hours*60)
#out format = "days-hours:minutes:seconds"
out = '{}-{}:{}:00'.format(days, hours, mins)
return out
n = int (input())
day = int (n // 1440)
hours = int (n % 1440) // 60
mins = int (n % 1440) % 60
print(day)
print(hours)
print(mins)

Algorithm needed to calculate difference between two times

I have an hour selection drop down 0-23 and minutes selection drop down 0-59 for Start time and End time respectively (so four controls).
I'm looking for an algorithm to calculate time difference using these four values.
Since they're not stored in fancy date/time selection controls, I don't think I can use any standard date/time manipulation functions.
How do I calculate the difference between the two times?
This pseudo-code gives you the algorithm to work out the difference in minutes. It assumes that, if the start time is after the end time, the start time was actually on the previous day.
const MINS_PER_HR = 60, MINS_PER_DAY = 1440
startx = starthour * MINS_PER_HR + startminute
endx = endhour * MINS_PER_HR + endminute
duration = endx - startx
if duration < 0:
duration = duration + MINS_PER_DAY
The startx and endx values are the number of minutes since midnight.
This is basically doing:
Get number of minutes from start of day for start time.
Get number of minutes from start of day for end time.
Subtract the former from the latter.
If result is negative, add number of minutes in a day.
Don't be so sure though that you can't use date/time manipulation functions. You may find that you could easily construct a date/time and calculate differences with something like:
DateTime startx = new DateTime (1, 1, 2010, starthour, startminute, 0);
DateTime endx = new DateTime (1, 1, 2010, endhour , endminute , 0);
Integer duration = DateTime.DiffSecs(endx, startx) / 60;
if (duration < 0)
duration = duration + 1440;
although it's probably not needed for your simple scenario. I'd stick with the pseudo-code I gave above unless you find yourself doing some trickier date/time manipulation.
If you then want to turn the duration (in minutes) into hours and minutes:
durHours = int(duration / 60)
durMinutes = duration % 60 // could also use duration - (durHours * 60)
This will compute duration in minutes including the year as factor
//* Assumptions:
Date is in Julian Format
startx = starthour * 60 + startminute
endx = endhour * 60 + endminute
duration = endx - startx
if duration <= 0:
duration = duration + 1440
end-if
if currday > prevday
duration = duration + ((currday-preday) - 1 * 1440)
end-if
First you need to check to see if the end time is greater than or equal to the start time to prevent any problems. To do this you first check to see if the End_Time_Hour is greater than Start_Time_Hour. If they're equal you would instead check to see if End_Time_Min is greater than or equal to Start_Time_Min.
Next you would subtract Start_Time_Hour from End_Time_Hour. Then you would subtract Start_Time_Min from End_Time_Min. If the difference of the minutes is less than 0 you would decrement the hour difference by one and add the minute difference to 60 (or 59, test that). Concat these two together and you should be all set.
$start_time_hr = 5;
$start_time_mi = 50;
$end_time_hr = 8;
$end_time_mi = 30;
$diff = (($end_time_hr*60)+$end_time_mi) - (($start_time_hr*60)+$start_time_mi);
$diff_hr = (int)($diff / 60);
$diff_mi = (int)($diff) - ($diff_hr*60);
echo $diff_hr . ':' . $diff_mi;
simple equation should help:
mindiff = 60 + endtime.min - starttime.min
hrdiff = ((mindiff/60) - 1) + endtime.hr - starttime.hr
This gives you the duration in hours and minutes
h1 = "hora1"
m1 "min1"
h2 "hora2"
m2 = "min2"
if ( m1 > m2)
{
h3 = (h2 - h1) - 1;
}
else
{
h3 = h2 - h1;
}
m1 = 60 - m1;
if (m1 + m2 >= 60)
{
m3 = 60 - (m1 + m2);
} else if (m3 < 0)
{
m3 = m3 * -1;
}
else
{
m3 = m1 + m2;
}
System.out.println("duration:" + h3 + "h" + m3 + "min");
If you have a function that returns the number of days since some start date (e.g. dayssince1900) you can just convert both dates to seconds since that start date, do the ABS(d1-d2) then convert the seconds back to whatever format you want e.g. HHHH:MM:SS
Simple e.g.
SecondsSince1900(d)
{
return dayssince1900(d)*86400
+hours(d)*3600
+minutes(d)*60
+seconds(d);
}
diff = ABS(SecondsSince1900(d1)-SecondsSince1900(d2))
return format(diff DIV 3600)+':'+format((diff DIV 60) MOD 60)+':'+format(diff MOD 60);
Hum: Not that simple if you have to take into account the leap seconds astronomers are keen to put in from time to time.

Resources