Get time interval in minutes after last update - php

In a validation there needs to be an interval of 180 minutes between each update, and as a callback I want to display how many minutes it's left until it's available for "updating" again.
I have this code:
time() - $user['last_update']
It gives me the unix time difference in seconds ($user['last_update'] is in unix time)
How can I get time() - $user['last_update'] to show that is it i.e. "120 minutes left"?
Thankful for input

Try this!:)
echo ((time() - $user['last_update'])/60)." minutes left";

Use DateTime class:
$now = new DateTime();
$time = new DateTime($user['last_update']);
$interval = $time->diff($now);
echo $interval->format("%i minutes remaining");
Demo!

Related

if date time difference is over cetain amount

$now = new DateTime();
$future_date = new DateTime($date);
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Basically I want to use the difference between the two dates now and whatever the person has booked. I need to use an if statement to check if the difference is between 24 hours / 1 day.
how do i use datetime in if statements?
like
If ($interval > 24 hours) {
allow } my problem is how do i write 24 hours in php? sounds really dumb I know.
$now_ts = $now->getTime();
$future_date_ts = $future_date->getTime();
if ($future_date_ts - $now > 60 * 60 * 24) {
// more than 24 hours before $future_date
}
If you also want the diff the other way add:
$now - $future_date_ts > 60 * 60 * 24 // more than 24 hours after $future_date
This gives you a 48 hour range around $future_date.
You could instead use the UNIX timestamp ( the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
time(); will return the current Unix timestamp and you can convert your future_date to a timestamp using strtotime();
So you can calculate the difference in seconds using:
$diff_secs = strtotime($future_date) - time();
And knowing 1 day is 24 hours which is 24*60 minutes which is 24*60*60 seconds,
$diff_days = (strtotime($future_date) - time())/(24*60*60);
Or you could use the difference in hours as your comparison :
$diff_hours = (strtotime($future_date) - time())/(60*60);
if($diff_hours > 24) {
//do something if its more than 24 hours away
}

PHP Compare a difference of timestamp and a real time

I would like to compare a difference between two timestamps and a time in base 60 or 10.
More precisely if timestamp1-timestamp2 is longer (or no) than x hours and y seconds.
I'm using DateTime and DateInterval classes, but there isn't such a function, and i don't find a clean solution.
Thanks
$time1 = new DateTime("2011-01-26 01:13:30"); // string date
$time2 = new DateTime();
$time2->setTimestamp(1327560334); // timestamps, it can be string date too.
$interval = $time2->diff($time1);
echo $interval->format("%H hours %i minutes %s seconds");
Output
11 hours 32 minutes 4 seconds
$timestamp1 = strtotime('2012-01-26 14:00:00');
$timestamp2 = strtotime('2012-01-25 17:00:00');
if (abs($timestamp1 - $timestamp2) < 60 * 60 * 5 /* (5 hours) */) {
...
Convert both the timestamp and the real time to the UNIX timestamp. Then simply subtract to get the number of seconds difference.
With datetime objects:
$interval = $TempReceiptDateTime->diff($ShipDateTime);
echo $interval->format('%R%H:%I:%s days');
more

PHP check time is less than a specific value?

I am trying to compare times and to see if it is less than a set time and if so, do something...How can this be achieved?
For example I have this so far.
$now = strtotime(date('g:i a'));
$event_time = strtotime("10:00 am");
With these two variables, how can I check if the event_time is 5 minutes BEFORE the current "now" time?
Thanks...
You don't need strtotime for the current time. Timestamps are values in seconds, just do some math:
$now = time();
$event_time = strtotime("10:00 am");
if( ($now - $event_time) == (5 * 60)) // 5 minutes * 60 seconds, replace with 300 if you'd like
{
// 5 minutes before
}
Demo - But you should use the above code for the $now variable.

Get future timestamps

Would it be possible to get the unix timestamp 7 days from now?
Would be awesome!
Yes, get the unix timestamp and add 25200 to it. If you want to format that timestamp you can use date().
$future = time() + (60 * 60 * 24 * 7);
date("o", future);
And from the PHP docs for time()
date('Y-m-d', strtotime('+1 week'))
Sure.
Get the current timestamp. Add 7 days worth of seconds.
Note: The timestamp "7 days ahead" (in terms of 7 * 86400 seconds) of the current timestamp may not represent the same day-of-week or the same hour in the day (yay daylight savings!) or even the same second (rare, yay leap-seconds!).
time() + (60 * 60 * 24 * 7); // "good enough"
strtotime('+7 days'); // daylight savings save
Just add seven days?
$future = time() + 60*60*24*7;
// seconds ---^ ^ ^ ^
// minutes ---^ ^ ^
// hours ---^ ^
// days ---^
See time().... oh, the example given there does exactly what you want... I guess you have not read the manual before.
The proper way of doing this on a recent version of PHP is using the DateTime object (in my opinion).
$date = new DateTime('now'); // can be anyting else too
$date->modify('+1 week');
// PHP 5.3
$future = $date->getTimeStamp();
// PHP 5.2
$future = $date->format('U');
$now = time() + (7 * 24 * 60 * 60);
Get the current unix timestamp using time() and then multiply 60 seconds, 60 minutes, 24 hours, and 7 days.

How to get duration in terms of minutes by subtracting a previous time stamp from the present time in PHP?

How to get duration in terms of minutes by subtracting a previous time stamp from the present time in PHP?
The format of time stamp is like
2009-12-05 10:35:28
I want to calculate how many minutes have passed.
How to do it?
To do this in MySQL use the TIMESTAMPDIFF function:
SELECT TIMESTAMPDIFF(MINUTE, date_lastaccess, NOW()) FROM session;
Where session is the table and date_lastaccess is a DATE / DATETIME field.
If you don't wanna add a library, you can do this pretty easily:
<?php
$date1 = "2009-12-05 10:35:28";
$date2 = "2009-12-07 11:58:12";
$diff = strtotime($date2) - strtotime($date1);
$minutes_passed = $diff/60;
echo $minutes_passed." minutes have passed between ".$date1." and ".$date2;
?>
Check out some pretty date libraries in PHP. Might have something for you. Here's one : http://www.zachleat.com/web/2008/02/10/php-pretty-date/
strtotime("now") - strtotime("2009-12-05 10:35:28")
That will get you seconds difference. Divide by 60 for minutes elapsed. If you have any information on the time zone of the initial timestamp, however, you'd need to adjust the "now" to a more comparable time
something like that
$second = strtotime('now') - strtotime($your_date);
$minute = $second / 60;
strtotime return the number of seconds since January 1 1970 00:00:00 UTC, so you can easily manipulate this. if you need don't want to have 35.5 minute you can use floor to round up the number. Last remark both time need to be in the same timezone or you are going to count the timezone difference also.
You could just use
$timestamp = time();
$diff = time() - $timestamp
$minutes = date( i, $diff );
function timeAgoMin($timestamp){
if(!is_int($timestamp)){
$timestamp = strtotime($timestamp);
}
return ((time() - $timestamp) / 60);
}

Categories