PHP check time is less than a specific value? - php

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.

Related

Comparing datetime to current time using PHP

Given $some_date equal to 2015-02-12 10:28:04, how can I determine whether it is not older than X hours of the current time using PHP (and not MySQL)?
strtotime and the DateTime class are our friends. Where $x is hours:
if(($time = strtotime($some_date)) > $time + ($x * 360)) {
//do something its more than X hours
}
That likely won't work across daylight savings time boundaries, so maybe:
if(($time = strtotime($some_date)) > strtotime("+$x hours", $time)) {
//do something its more than X hours
}
This will determine if your date is less than 2 hours from the current datetime
<?php
$date = new DateTime(); //set current datetime to variable
$date->setTimezone(new DateTimeZone('America/Detroit')); //set timezone if you like
$fdate = $date->format('Y-m-d H:i:s'); //change format to year - month - day, hour, minute, seconds
$some_date = strtotime('2015-02-13 18:30:04'); // this is your datetime. use your time or change $some_date to your variable
$new_date = strtotime($fdate) - $some_date;
$minute_date = $new_date / 60; // convert to minutes
$hour_date = $minute_date / 60; // convert to hours
print $hour_date;
if($hour_date > 2){ // change the 2 to whatever you want it to be.
print "more than two hours";
}else{
print "less than two hours";
}
?>

Get time interval in minutes after last update

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!

Create Variable in PHP Equal to Current Time Minus One Hour

In PHP, how could I create a variable called $livetime that equals the current time minus 1 hour?
Another way - without all the math and, in my opinion, reads better.
$hour_ago = strtotime('-1 hour');
If you're looking for how to display the time in a human readable format, these examples will help:
$livetime = date('H:i:s', time() - 3600); // 16:00:00
$livetime = date('g:iA ', time() - 3600); // 4:00PM
$livetime = time() - 3600; // 3600 seconds in 1 hour : 60 seconds (1 min) * 60 (minutes in hour)
See time PHP function for more information.
convert your date to strtotime and then subtract one hour from it
$now = date('Y-m-d H:i:s');
$time = strtotime($now);
$time = $time - (60*60); //one hour
$beforeOneHour = date("Y-m-d H:i:s", $time);
You could use the date_create function along with the date_sub function like I have shown here below: -
$currentTime = date_create(now());
$modifyTime = date_sub($currentTime,date_interval_create_from_date_string("1 hour"));
$liveTime = $modifyTime->format('Y-m-d H:i:s');
Assuming that a timestamp is fine you can use the time function like so
<?php
$livetime = time() - 60 * 60;
Current time is equal to time() (current time given in seconds after Unix epoch).
Thus, to calculate what you need, you need to perform calculation: time() - 60*60 (current time in seconds minus 60 minutes times 60 seconds).
$time_you_need = time() - 60*60;
First convert hours into seconds (3600) then use the following:
$your_date = date('F jS, Y',time() - 3600);

Difference between dates

I want to calculate the difference between two times, one of which is the current time, and the other is just in the format HH:MM, always in the future.
If I just subtract $futuretime from $now, it should, of course, be a positive number.
This works fine until...
If $now is in the afternoon or evening and $futuretime is, say, 7AM next morning, how can I force it to understand the the time is always going to be in the future?
(It's for working out the time of something that occurs about every half an hour during working hours, and then stops until the following morning)
Thanks in advance!
Simply add a whole day to the difference if it is negative. Assuming that $now and $futuretime are timestamps (stored in seconds since midnight), simply do the following:
$diff = $futuretime - $now;
if ($diff < 0)
$diff += 24 * 3600;
If they are in HH:MM format:
list($future_hours, $future_mins) = explode(":", $futuretime);
$futuretime = $future_hours * 60 + $future_mins;
list($now_hours, $now_mins) = explode(":", $now);
$now = $now_hours * 60 + $now_mins;
$diff = $futuretime - $now;
if ($diff < 0)
$diff += 24 * 60;
Of course the latter case returns the difference in minutes, while the former returns the difference in seconds.
Edit: as Andy noted below in the comments, this is not a good solution if you care about changes in DST. See his solution for a better approach.
If you're on PHP 5.3+, use PHP's DateTime:
$d1 = new DateTime('14:52:10');
$d2 = new DateTime('12:12:10');
$diff = $d1->diff( $d2 );
var_dump( $diff );
You can simply convert both dates to timestamp, do the calculations and convert it to required format, i.e. days, hours and minutes. it's quite simple.

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