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

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;

Related

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.

how to increase an integer every specific minute in 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

PHP get 00:00:00 of a unix timestamp

If I got a unix time which is e.g
1407050129
How do I get the 12:00AM of that unix day in unix timestamp , means the first minute of the day of that unix time.
Example if i want get today first minute
$today_first_min = strtotime("00:00:00");
Try this:
$that_day = "1407050129";
$that_day_first_min = strtotime(date('Y-m-d', $that_day) . ' midnight');
See demo
An alternate method to arrive at the same result... Unix time is a count of seconds since 1970-01-01 00:00:00 UTC, so each whole day is a multiple of (60*60*24) seconds.
Using this fact you can use the modulus operator (%) to calculate and then remove the remainder (ie. the seconds, hours and minutes) from the day, and get back to the first hours!
date_default_timezone_set('UTC');
$that_date = 1407050129;
$first_hour = $that_date - ($that_date % (60*60*24));
print date('Y-m-d H:i:s', strval($first_hour));
// 2014-08-03 00:00:00

Php calculate hours elapsed from PM to AM

Time is confusing... How can one calculate the time elapsed from PM to AM in php?
For example 22:00:00 (10pm) to 02:00:00 (02am) should give 04 hours elapsed. Instead my code returns -08 hours
function hours_elapsed()
{
$timestart = strtotime("22:00:00");
$timestop = strtotime("02:00:00");
$time_diff = $timestop - $timestart; //time difference
return gmdate("h", $total_hours);
}
It's clear that the code calculates in 24 hour format so of course it returns -08 but how is it possible to get time elapsed without 24 hour constraint.. when it passes the midnight mark?
-8 hours is 4 hours. Just add 12 if the number is negative:
$time_diff = $timestop - $timestart; //time difference
if ($time_diff < 0) {
$time_diff += 12;
}
This won't help you, however, if your dates are more than one day apart. You need to specify the date as well (as in the day of the month) to tell PHP that those times are different.
Clearly 22:00:00 is 24 hour clock, 02:00:00 could be either though couldn't it?
add the date as well, or test for the negtive and add 12 hours.
Quick solution from me: add a day to the later date.
echo gmdate('h', strtotime('1970-00-01 02:00:00') - strtotime('1970-00-00 22:00:00'));
But Blender's answer seems to be easier. :)

Timestamp intervals processed by date() don't calculate correctly

I have two unix timestamps in my database that I am subtracting to get a time interval in seconds:
$interval = $array["time2"] - $array["time1"]; // When echoed, $interval = 3
However, when I run this $interval through date(), like so:
echo date("g\h i\m", $interval);
these 3 seconds all of a sudden echo to:
7h00m
Does anyone have any idea why date() might be taking these three seconds and stretching them out into a 7 hour interval somehow?
The second argument to date() is a timestamp (seconds since midnight, Jan 1, 1970 GMT). Your interval is probably equating to 7am in your timezone relative to this date.

Categories