Comparing timestamps in PHP - php

How would I be able to return a boolean expression of true once exactly 24 hours have passed and no more?
I am using strtotime() to out put these two times:
$time1 = 1316268919;
$time2 = 1316268898;

This should do what you need:
if (($time1-$time2) == 86400) {
// Exactly 24 hours have passed.
}

strtotime() returns the number of seconds since the Unix epoch. So to find the difference in times, simply subtract one from another. Then compare that difference against the number of seconds in 24 hours (24 * 60 * 60).

abs($time1 - $time2) === 60 * 60 * 24
the abs is just in case; time2 might be bigger than time1.
If the difference in times equals 60 (seconds in a minute) * 60 * (minutes per hour) * 24 (hours for a day) = 86400, there's a day difference

24h=86400sec so, when difference of two is 86400, it's exactly 24h
n.b. twice a year could cause issue do to DST, if applicable.

Related

Converting a Total Time to Seconds

I'm totalling up time like so.
$totalTimeDiff = new DateTime("#0");
foreach($dbrecords as $row)
{
$timeDiff = date_diff( ... two datetimes from my database ... )
$totalTimeDiff->add($timeDiff);
}
So $totalTimeDiff is a DateTime object with the sum of all of the time differences added together (so a sum of all of the durations). How can I get the total time in seconds?
Why not keep it simple?
$totalseconds=0;
foreach($dbrecords as $row)
$totalseconds+=(UNIX_TIMESTAMP(second_datetime)-UNIX_TIMESTAMP(first_datetime));
use strtotime function
echo strtotime('01:00:00') - strtotime('TODAY');
$totalTimeDiff->format('U');
Taking moonwave99's advice, I used DateInterval (can't remember why I went with DateTime for that in the first place, possibly a workaround for something at another stage of the project) and computed the seconds by adding each value to the total after converting it to seconds (converting hours and minutes to seconds and summing them up). I did this by using the DateInterval class's seconds property as well as the following function to convert a DateInterval to seconds (Note: only accounted for days, hours, minutes, and seconds for my specific case as there's no chance the amount will exceed one month):
function convertDateIntervalToSeconds($dateInterval)
{
$days = $dateInterval->d * 24 * 60 * 60;
$hours = $dateInterval->h * 60 * 60;
$minutes = $dateInterval->i * 60;
$seconds = $dateInterval->s;
return $hours + $minutes + $seconds;
}

Maths on PHP Timestamps

I have the following block of PHP:
if ($data->timestamp > (time() - 10 * 60) ) {
// data is newer than 10 mins
} else {
// data is older than 10 mins
}
That basically says if the timestamp stored in $data is older than 10 minutes.
I'm not sure how this is worked out exactly, and I've read about the time function here: http://php.net/manual/en/function.time.php
I want to edit it to become every hour instead of 10 minutes.
Can anyone share some advice/resources? e.g. how I would change it to become 1 hour and how exactly this is worked out.
This code line checks if the timestamp saved in the $data is NOT older than 10 minutes.
the time() function gives UNIX tiemstamp of the machine in seconds. If you want to change it to an hour its 60 minutes in 60 seconds 60*60. Your code will be like this:
if ($data->timestamp > (time() - 60*60) )
it is worked out starting from the right to left in seconds. One hour would be
if ($data->timestamp > (time() - 60 * 60);
Formatt
time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
You are multiplying it as you work back 60 multiplied by 60 minutes etc etc

How to get current time elapsed percentage of today? [duplicate]

This question already has answers here:
Work out the percentage of the day that has elapsed
(5 answers)
Closed 9 years ago.
Im trying to get the current time elapsed percentage of todays time. It can be either javascript or php. How can I do that ?
Use a Date object and some arithmetic. Convert the components of the current day into a equivalent unit (such as seconds), find their sum, and divide that by the number of that unit per day.
For example, the following is a solution in JavaScript.
var d = new Date();
var pctDayElapsed = (d.getHours() * 3600 + d.getMinutes() * 60 + d.getSeconds() + d.getMilliseconds()/1000)/86400;
Note that this approach piggybacks on the browser's localization. Your result will depend on the browser's timezone.
I would use the current time to calculate the number of seconds that has elapsed today and then divide that by 86400 (the number of seconds in a day), and of course multiply that by 100 to get it from decimal to percent.
Here is an answer in PHP:
$now=time();
$today=strtotime(date("m/d/Y"));
$seconds=$now-$today;
$day=24*60*60;//seconds in a day;
$percent=$seconds/$day*100;
OR
$hours=date('G')*60*60;
$minutes=date('i')*60;
$seconds=date('s');
$day=24*60*60;//seconds in a day;
$percent=($hours+$minutes+$seconds)/$day*100
<?php
$timestamp = time();
$hours = intval(date("G", $timestamp));
$minutes = intval(date("i", $timestamp));
$seconds = intval(date("s", $timestamp));
$proportion_elapsed = ($hours * 60 * 60 + $minutes * 60 + $seconds) /
(24 * 60 * 60);
printf("%0.4F of the day has elapsed.", $proportion_elapsed);
?>
This works as long as there are 86,400 seconds in a day, which may not be true due to Daylight Savings time or leap seconds.
Round down to the beginning of the day by dividing the current time by 86400, then multiply that integer value by 86400. Then take the difference of the current time (in seconds of course) and then divide 86400 into it. Lastly, multiply by 100.
Edit: mod works more efficiently
Pseudocode:
MidnightDays = (TimeInSeconds % 86400) * 86400;
Percentage = (TimeInSeconds - MidnightDays) / 86400 * 100;

php date conversion

How would i convert a format like:
13 hours and 4 mins ago
Into something i can use in php to further convert?
try
strtotime('13 hours 4 mins ago', time())
http://php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/datetime.formats.php
http://www.php.net/manual/en/datetime.formats.relative.php
To get a unix timestamp fromt he current server time:
$timestamp = strtotime("-13 hours 4 minutes");
Try the DateInterval class - http://www.php.net/manual/en/class.dateinterval.php - which you can then sub() from a regular DateTime object.
Do it the hard way:
$offset = (time() - ((60 * 60) * 13) + (4 * 60));
Working Example: http://codepad.org/25CJPL76
Explanation:
(60 * 60) is 1 hour, then * 13 to make 13 hours, plus (4 * 60) for the 4 minutes, minus the current time.
What i usually do is create several constants to define the values of specific time values like so:
define("NOW",time());
define("ONE_MIN", 60);
define("ONE_HOUR",ONE_MIN * 60);
define("ONE_DAY", ONE_HOUR * 24);
define("ONE_YEAR",ONE_DAY * 365);
and then do something like:
$offset = (NOW - ((ONE_HOUR * 13) + (ONCE_MIN * 4)));
in regards to actually parsing the string, you should look at a javascript function and convert to PHP, the source is available from PHP.JS:
http://phpjs.org/functions/strtotime:554
Use strtotime(), and perhaps date() later, e.g.:
$thetime = strtotime('13 hours 4 minutes ago');
$thedate = date('Y-m-d', $thetime);

PHP time substraction

I am trying to substract 24 hours time format and then convert it into minutes but it does not work well.
Here is my code
$time1 = '2010-08-05 23:00:00';
$time2 = '2010-08-05 00:00:00';
echo round( (strtotime($time2) - strtotime($time1)) / 60);
it will display this -1380.
if you put 1-23 hour in time2 it will work. I tried to convert time in 12 hours but it didn't work.
Any help would be greatly appreciated.
The result is in minutes not in hours.
strtotime() returns a result in seconds (in the Unix timestamp format). You will need to divide by 3600 to convert to hours.
Try this:
$time1 = '2010-08-05 23:00:00';
$time2 = '2010-08-05 00:00:00';
echo abs ( round( (strtotime($time2) - strtotime($time1)) / 3600) );
The abs() function returns an absolute value. In this case, the final result is 23 hours.
I think this is what you are trying to get:
echo round( (strtotime($time1) - strtotime($time2)) / 3600);
Firstly you were subtracting the times the wrong way round, then you were only dividing by 60 which was giving you your answer in minutes.

Categories