Number of seconds until every 20 minutes - php

For a game I'm making, each turn is at :00, :20, and :40 past the hour. I am trying to create a countdown that will display the number of minutes (and eventually seconds) to the next turn.
My current approach is to store the time of the next turn in a database, then doing
$time = time() - $time_to_turn
where time_to_turn is the database entry of the next turn time.
Is there a way to not use the database?

Try $minutesRemaining = 20 - date('i') % 20; or $secondsRemaining = 1200 - date('s') % 1200

Related

how to calculate time spent on my app by timestamp

i have one column timestamp when user enters the app and another column when user leaves the app . i want to calculate the time spent on the app :
sum(timestamp_exit) - sum (timestamp_enter) .
right now i've tried to right the current query :
select (SUM(unix_timestamp(`created_time_enter`))) as enter , (SUM(unix_timestamp(`created_time_exit`))) as exit
FROM `my_table`
but i get large numbers and i don't know if it's the correct way. any suggestion?
You could calculate this using the timeDiff function:
times = array();
foreach ($result as $row){
// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);
// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;
$times[] = $timeDiff;
echo(secondsToTime($timeDiff));
# 18 days, 23 hours, 41 minutes and 7 seconds
}
echo(secondsToTime(array_sum($times)));
#total of all times

Time calculation activity in Php

I am working on a web application, in which a web user activity is to be block form half hour before of given time.
I need to calculate time half hour before of given time.
Means if a date and time give like 08/04/2015 16:00:00
now need to calculate time half hour before of the given time i.e. 08/04/2015 15:30:00 during this a web activity is blocked for end user.
Please give me suggestion and sample code in PHP.
you can take this for your refrence,
but you need to modify the code as per your requirements
$start_time = strtotime("2008-12-13 10:42:00"); // get this time while user loggs in
$end_time = strtotime(date('Y-m-d H:i:s'));// this is dynamic time, it changes everytime when the page is reloaded
$difference_in_minutes = round(abs($end_time - $start_time) / 60,2); // this will return the diference between two times in minutes
if($difference_in_minutes >= 30)
{
// do you 30 minutes block stuff here
}
let me know if any further classification needed

Countdown timer hour reset with history

I'm trying to make countdown timer that resets itself every 60 minutes and wont reset on every refresh.
I think the ideal way to do this is via timezone?
There are multiple ways to perform this, but usually i'd recommend storing the unix-timestamp in a MySQL Database, then fetch and compare the previous/stored unix-timestamp with the current timestamp. The PHP-function time() will get you the current timestamp.
If the entire project is about making a counter, you can probably achieve the same effect by writing/loading from a text-file. The same goes for sessions.
Here's a little snippet to give you an idea of how to solve this:
if (($unixtime + (60 * 60)) > time()) //previous time + 60 * 60 sec (60 min).
{
//update counter
}
//present counter

Converting Epoch time to an H:mm timestamp in PHP

This seems like there should be a very easy solution, however everything I'm trying is either giving me an error or the wrong result.
I'm pulling data from a MySQL table and the time is stored in the Epoch format in the database. When I make the query on the website it's showing: 3672 (the same number shown in the database). I've tried using the date() function, a number of different str* functions, different arithmetic operations, however nothing is giving me the actual time, which should be showing as: '1:02'.
I'm not trying to pull the date, actual time, etc. I'm just trying to convert an Epoch time string to a traditional 'H:mm' format, because these are for durations, not timestamps.
Any help would be greatly appreciated!
As others already noticed, this is not really any standard epoch time but simply number of seconds since midnight.
Looking at your example, you only need hours and minutes (rounded up). This will give you those:
$h = (int)($number / 3600);
$m = ceil(($number - $h * 3600) / 60);
$result = sprintf('%d:%02d', $h, $m);
Dividing number by 3600 (60 for seconds * 60 for minutes) will give you number of hours. Using your example of 3672 this will give you 1.
To get minutes you just remove hours to get seconds (72) and then divide that by 60 to get minutes (1 minutes and 12 seconds). Since your example specifies 1:02 is result, you can simply take next upper integer (2).
At end result is 1:02, as you specified.

Rounding time to nearest quarter, atleast 30 minutes in time

Bah, I've been fiddling on how to do this. I need a function that returns <hour>:<minutes> that is rounded to a quarter, but need to be atleast 30 minutes in future time.
Anyone got a good idea and how to do this?
Add 30 minutes.
Extract date + hour on the one hand side, minutes on the other.
Divide minutes by 15, ceil the result, multiply by 15.
Build new date using date + hour and add the new minutes.
$current = date("Hi");
$ceilUp = ceil($current / 15);
$timeNeeded = $ceilUp + 30;
something like that? (I see it's not exactly what you need.. but this is a start).

Categories