how to increase an integer every specific minute in php - php

I need a php code that when you type (time), and a (base number), it increases by base number value, on specific time intervals.
e.g:
every 40 minutes, add 0.0001 to the base number 0.04
0.04
0.0401
0.0402
.....

You can use the unixtime and calculate how many 40 minutes that has elapsed.
$start = 1537088883; //Unix time of when I started writing answer.
$elapsed = time() - $start; // elapsed seconds since start
$counts = floor($elapsed/(60*40)); // number of 40 minutes that has elapsed
echo 0.04 + (0.0001*$counts);
https://3v4l.org/q9Vhm
Right now it will output just 0.04, but if we manipulate the $start we will get a different output ( or just wait 40 minutes and the same code will output a different number).
https://3v4l.org/E9XWN
If you want to set a start date and time you can use strtotime to convert a human readable date to Unix time.
$start = strtotime("2018-09-01 09:00:00");
https://3v4l.org/c6g0l

Related

How to get missing time to a microtime from another one, formatted (H:i:S) with PHP?

i am new to stackOverflow, i was wondering how could i get the missing time date to a microtime(true) starting from another microtime(true), and get it formatted like that (H:i:s).
This is my code:
$rewardCountdown = microtime(true) - $this->dailyRewardTime; // daily reward is another microtime(true)
$rewardAvailable = $rewardCountdown >= 60 * 60 * 24; // check if 24h are gone so we can get reward
Basically i want to get the $rewardCountdown in this format (H:i:s)
I tried and somehow got something like that but i was getting the time increasing instead of decreasing
The current microtime minus a previous microtime would be a positive value, of the number of seconds passed between the two times.
If you want to convert seconds into Hours, Minutes and Seconds, you can simply use gmdate.
$rewardCountdown = microtime(true) - $this->dailyRewardTime;
$date = gmdate('H:i:s', $rewardCountdown);

PHP DateTime diff returning incorrect value in minutes

I have this PHP code which calculates the time between 2 timestamps and displays it in minutes...
$timestamp1 = new DateTime();
$timestamp1->setTimestamp('1540718680');
$timestamp2 = new DateTime();
$timestamp2->setTimestamp('1540747360');
$since_start = $timestamp1->diff($timestamp2);
echo $since_start->i.' minutes<br>';
For some reason it is returning 58 minutes insead of 477 minutes.
Where am I going wrong?
%i gives only the minutes, it's like asking for the 10s digit in a 3 digit number. You also need the hours since minutes is just the remainder after the larger units are subtracted.
You could also do the computation yourself, b-a/60, to get the number of minutes between the two specified timestamps.

Display Database Entities within a specific time frame

I want to display content from the database with dates up to 2hours ahead of time.
Example:
2018-11-09 20:00:00.000000
2018-11-08 19:00:00.000000
2018-11-06 19:00:00.000000
2018-11-06 18:00:00.000000
Lets say the time and date is
Nov 6th at 6pm. I want the bottom two entries to be displayed and the two future dates to not show until the current time is within 2hours of that time.
My code is as follows:
$cT = strtotime($row3['MissionTime']) - strtotime("now");
if($cT <= strtotime('-2 hours')) {
echo $row3['MissionTime']."<br>";
}
I've tried several different ways but I can't seem to get this to work right. Help and tips?
The reason your code doesn't work is that strtotime returns a number of seconds since the unix epoch. When you subtract two results of strtotime you will get a number of seconds difference which is as you expect. However you cannot compare that value to strtotime('-2 hours') as the output of that will be the timestamp for 2 hours before now (which right now is 1541539906), so the test will always pass. You should just compare it to 7200 instead (I'm pretty sure based on your question description that +7200 is more appropriate than -7200). so change
if($cT <= strtotime('-2 hours')) {
to
if($cT <= 7200) {
Note that it is almost certainly better to do this in your query. Try adding a condition on your time column as something like
WHERE MissionTime <= NOW() + INTERVAL 2 HOUR
And then you won't need to check in the PHP at all.
strtotime() returns a timestamp in seconds. Subtracting two timestamps gives you a difference between those two timestamps, in seconds.
So if strtotime($row3['MissionTime']) is a timestamp that's 1.5 hours in the future, and you subtract strtotime("now") from it, you end up with a difference of 5400 seconds (60 seconds * 60 minutes * 1.5 hours).
strtotime('-2 hours') gives you the timestamp for 2 hours ago, which is currently somewhere around 1.5 billion. This is not very useful for your situation.
Here are two ways to modify your code:
$cT = strtotime($row3['MissionTime']) - strtotime("now");
if($cT <= 7200) {
echo $row3['MissionTime']."<br>";
}
If the difference between $row['MissionTime'] and now is less than 7200 seconds (60 seconds * 60 minutes * two hours), $row3['MissionTime'] is either in the past or it's within the next two hours.
Alternatively:
if(strtotime($row3['MissionTime']) <= strtotime('+2 hours')) {
echo $row3['MissionTime']."<br>";
}
Basically the same, but perhaps more readable if you're not planning to use $cT for anything else. This simply checks if $row3['MissionTime'] is earlier than whatever time it will be in +2 hours.

What does the result of the time() function mean?

When I call the time() function, it returns an integer. What is the meaning of this number?
For example, running the program:
$time = time();
echo $time;
Gives the output:
1452082553
Also, how would I add a duration onto that time, for example half an hour?
You get half an hours by,divide it by 60 to get minutes and by 60 again to get hours.
The format is the so called unix timestamp which counts the seconds from January 1st of 1970 00:00 o'clock.
To calculate 1/2 hour add/subtract 30 minutes * 60 seconds = 1800.
$halfAnHourago = time() -1800;

Find difference between now and a file's last change time in PHP

I have a file called "file.txt".
I get its last change time with this:
$lastTime = filemtime("file.txt");
Then I make
$lastDate = date("Y-m-d",strtotime( $lastTime ));
$todaysDate = date("Y-m-d",strtotime('now'));
Then I substract lastDate from todaysDate and find the difference.
Is there a quicker way to do this?
I don't want to check whether the difference of seconds is >= 86400 or not.
For example the difference between these two dates must be "1".
2013-03-31 10:00
2013-03-30 19:00
If i check for the difference of seconds I will have 54K seconds, which is smaller than 86400 seconds.
Get the difference in seconds, then convert to days and round the result.
$diff = time()-filemtime("file.txt");
$days = round($diff/86400);
You could also use floor or ceil in place of round depending on exactly how you want to handle partial days.

Categories