PHP Compare a difference of timestamp and a real time - php

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

Related

How to check if the difference between 2 dates is bigger than 15 minutes?

I am using this function to compare a date from the database to the current date, and i need to check if the difference between the 2 dates is bigger than 15 minutes but i don't know how to do that, i think i need to do something like if($comp > 0 days 0 hours 15 minutes)
function TimeOut($dateP){
$date = new DateTime(date('Y-m-d H:i:s'));
$date2 = new DateTime($dateP);
echo $comp = $date->diff($date2)->format("%d days %h hours and %i minuts %s seconds");
if ($comp > "15 minutes ?") {
return true;
}
}
You can use diff and then read the m parameter of the result. In the example below $difference will be DateInterval object:
$difference = $start_date->diff($date2);
if($difference->i > 15) {
echo "difference greater than 15 minutes"
}
A date interval stores either a fixed amount of time (in years,
months, days, hours etc) or a relative time string in the format that
DateTime's constructor supports.

Find the difference between two time stamps

$diff = strtotime(12:00:00) - strtotime(5:01:29);
echo date('H:i:s', $diff);
result is 12:01:29
Trying to get the result of 5 hrs 1 min 29 sec
I prefer to use the DateTime class for time comparison / addition / subtraction:
$dt1 = new DateTime();
$dt1->setTimestamp(strtotime('12:00:00am'));
$dt2 = new DateTime();
$dt2->setTimestamp(strtotime('5:01:29am'));
$interval = $dt1->diff($dt2);
echo $interval->format('%h hrs %i min %s sec');
Edit: with the format you gave in strtotime it was thinking 5:01am to 12pm - so it was giving a very different answer than what you wanted, so you need to specify am/pm

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
}

How to get rough age of a timestamp/datetime object?

In PHP, if I have a datetime object or timestamp as properties of an object, how can I get a rough age for the record in PHP.
So that instead of a formated date string it says something like"about 1 hour ago", "3 days ago", "1 year ago" etc.A bit like redmine has for task comments, for example.
What is this process called?I tried searching for age and datetime and keep finding code to get someone's age of birth, which is not what I'm looking for.
if you have 2 objects. You need to convert first object to timestamp and second object is time();
time() - Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Then you substract time() with date you want and you get difference in seconds. To make minutes from seconds you need divide it by 60, to make hours you need to divide it by 60 * 60 = 3600 etc.
$secondsAgo = time() - $timeStamp;
$minutesAgo= (time() - $timeStamp) / 60;
$hoursAgo= (time() - $timeStamp) / 60*60;
$daysAgo= (time() - $timeStamp) / 60*60*24;
To change string to timestamp you can use strtotime()
The other solution is to use diff() on DateTime object.
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
This returns DateInterval object which has a lot of properties that are public
y Number of years.
m Number of months.
d Number of days.
h Number of hours.
i Number of minutes.
s Number of seconds.

php convert number into minutes

Say i have a variable like $speed = 5.5, what formula would I use to convert that into minutes, so in this case it would be 5 and a half minutes.
I need it to work in this:
date("Y-m-d H:i:s", strtotime("$now - $speed mins"));
Other examples, 2.25 would convert to 2 mins 15 secs, 7:75 to 7 mins 45 secs, etc
Anyone have any ideas? Never been a maths buff.
Just do it with second.
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', $speed * 60)));
If you want more precision, then
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', round($speed * 60))));
You could also use PHP's own DateInterval class (requires PHP 5.3) http://www.php.net/manual/en/dateinterval.createfromdatestring.php
With sample:
$interval = DateInterval::createFromDateString('5.5 minutes');
echo $interval->format('%Y-%m-%d %H:%i:%s')
Could also a Unix Timestamp for dates and 3600 = 1 hour.
For example, the current time would be: $timestamp = gmmktime();
So if you wanted to add ".5" (30 minutes) to the current time, you would say $timestamp + 1800. ".25" would be $timestamp + 900.
$minutes = floor($speed);
$seconds = ($speed - $minutes) * 60;

Categories