Calculate total seconds in PHP DateInterval - php

What is the best way to calculate the total number of seconds between two dates? So far, I've tried something along the lines of:
$delta = $date->diff(new DateTime('now'));
$seconds = $delta->days * 60 * 60 * 24;
However, the days property of the DateInterval object seems to be broken in the current PHP5.3 build (at least on Windows, it always returns the same 6015 value). I also attempted to do it in a way which would fail to preserve number of days in each month (rounds to 30), leap years, etc:
$seconds = ($delta->s)
+ ($delta->i * 60)
+ ($delta->h * 60 * 60)
+ ($delta->d * 60 * 60 * 24)
+ ($delta->m * 60 * 60 * 24 * 30)
+ ($delta->y * 60 * 60 * 24 * 365);
But I'm really not happy with using this half-assed solution.

Could you not compare the time stamps instead?
$now = new DateTime('now');
$diff = $date->getTimestamp() - $now->getTimestamp()

This function allows you to get the total duration in seconds from a DateInterval object
/**
* #param DateInterval $dateInterval
* #return int seconds
*/
function dateIntervalToSeconds($dateInterval)
{
$reference = new DateTimeImmutable;
$endTime = $reference->add($dateInterval);
return $endTime->getTimestamp() - $reference->getTimestamp();
}

DateTime::diff returns a DateInterval object between 2 dates.
The DateInterval object gives all the informations (the number of days, hours, minutes, seconds).
Here's a sample code:
/**
* intervalToSeconds
*
* #param DateInterval $interval
* #return int
*/
function intervalToSeconds(\DateInterval $interval) {
return $interval->days * 86400 + $interval->h * 3600 + $interval->i * 60 + $interval->s;
}
$date_1 = new \DateTime('2021-03-03 05:59:19');
$date_2 = new \DateTime('now');
$interval = $date_1->diff($date_2);
echo intervalToSeconds($interval);

You could do it like this:
$currentTime = time();
$timeInPast = strtotime("2009-01-01 00:00:00");
$differenceInSeconds = $currentTime - $timeInPast;
time() returns the current time in seconds since the epoch time (1970-01-01T00:00:00), and strtotime does the same, but based on a specific date/time you give.

static function getIntervalUnits($interval, $unit)
{
// Day
$total = $interval->format('%a');
if ($unit == TimeZoneCalc::Days)
return $total;
//hour
$total = ($total * 24) + ($interval->h );
if ($unit == TimeZoneCalc::Hours)
return $total;
//min
$total = ($total * 60) + ($interval->i );
if ($unit == TimeZoneCalc::Minutes)
return $total;
//sec
$total = ($total * 60) + ($interval->s );
if ($unit == TimeZoneCalc::Seconds)
return $total;
return false;
}

Related

Do action once but only repeat it after x time

So basically, i need to notify a user if:
a) sensor disconnects and there hasn't been an alarm sent in last $send_threshold
b) if alarm sent on same day and greater than $repeat_threshold.
Example values of the variables
$send_threshold = 12 * 60; // 12 min
$repeat_threshold = 2 * 60 * 60 + 45 * 60; // 2 hr 45 min
I'm drawing a complete blank on how to make it happen. The sensors are stateless so there is no way for me to check if a sensor is online, other than to check timestamps of received data that the sensor posted to the api.
/* FUNCTIONS */
function handleDisconnectAlerts($sensor,$dataset,$users,$settings)
{
end($dataset);
$last_timestamp = $dataset[key($dataset)]['timestamp'];
$now = time();
if($now > $last_timestamp && $now - $last_timestamp > $settings['disconnect_alarm'] * 60)
{
$send_threshold = $settings['disconnect_alarm'] * 60;
$repeat_threshold = $settings['disconnect_alarm_repeat_hours'] * 60 * 60 + $settings['disconnect_alarm_repeat_minutes'] * 60;
//not really sure what to do from here.
}
}
Well, i figured it out myself i guess. The initial check covers the intial interval of 60 * $settings['disconnect_alarm']. From there i just needed to check that an alarm hasn't been sent in $repeat_threshold time. Works like a charm.
function handleDisconnectAlerts($sensor,$dataset,$users,$settings)
{
end($dataset);
$last_timestamp = $dataset[key($dataset)]['timestamp'];
$now = time();
if($now > $last_timestamp && $now - $last_timestamp > $settings['disconnect_alarm'] * 60)
{
$repeat_threshold = $settings['disconnect_alarm_repeat_hours'] * 60 * 60 + $settings['disconnect_alarm_repeat_minutes'] * 60;
$n_sent = R::count('sensoralerts',' timestamp >=:time and field_name="disconnect" and sensor_id=:id ',
[':time'=>$repeat_threshold,':id'=>$sensor['id']]
);
if($n_sent == 0){
multiSendDisconnect($users,$sensor);
}
}
}

php - calculating distance divded by time

How do I divide a decimal by time queried from database as time format.
Any idea?
$time = date($entity->getTime()->format('H:i:s'));
$speed = $distance/$time
Which is definitely wrong and if my time is 00:40:00, I get some division by zero error.
I am unable to convert it to seconds because php takes DateTime from Time format in database.
I propose that you get your time in seconds, but you need to convert minutes and hours to seconds.
$seconds = date($entity->getTime()->format('s'));
$minutes = date($entity->getTime()->format('i'));
$hours = date($entity->getTime()->format('h'));
$time = $hours * 3600 + $minutes * 60 + $seconds;
$speed = $distance/$time;
Checkout strtotime() to convert it to seconds.
Docs: http://php.net/manual/en/function.strtotime.php
With strtotime it is a little bit tricky and only goes to 24:59:59.
Else use Voitcus solution.
$time = '00:40:00';
echo strtotime("1970-01-01 $time UTC");
1) Get time in seconds
function time2seconds($time='00:00:00')
{
list($hours, $mins, $secs) = explode(':', $time);
return ($hours * 3600 ) + ($mins * 60 ) + $secs;
}
$time = date($entity->getTime()->format('H:i:s'));
$timeInSeconds = time2seconds($time);
$distance = 40000;
$speed = $distance/$timeInSeconds;
2) If you are using MySQL database use function TIME_TO_SEC(time)
$time = date($entity->getTime()->format('H:i:s'));
$speed = $distance/$time
It's wrong. You must do:
$speed = $distance/$time * 3.6;
For instance this equation is valid 30km/H = 30000m * 3600 seconds * 3.6

Date Difference in days in php

I have a function to calculate the difference between two dates.
function getDateDifference($to, $from, $in) {
$diff = abs($to - $from);
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
if ($in == "days") {
return $days;
} else if ($in == "months") {
return $months;
} else if ($in == "years") {
return $years;
}
}
For the parameters i first convert the two dates into seconds like this,
checkin = '2012-07-26';
checkout = '2012-07-27';
check_in_date = strtotime(checkin);
check_out_date = strtotime(checkout);
im getting the correct difference when it comes to difference less than one month. But if the difference is more than one month, im always getting the difference as 1. Can someone tell me wat the problem is.
Currently, a month is always 30 * 60 * 60 * 24 sec, aka 30 days.
Your problem is that we're in July, and there are 31 days, not 30. You must take care of number of days per month.
You can make use of the DateTime class.
http://php.net/manual/en/datetime.diff.php
$checkin = new DateTime("2012-07-23");
$checkout = new DateTime("2012-07-27");
$difference = $checkin->diff($checkout);
echo "differrence = " . $difference->format('%R%a days');

PHP convert date interval diff to decimal

I'm trying to convert the difference between two dates into a total year count, right now I'm using this:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2010-10-10');
$interval = $datetime1->diff($datetime2);
return $interval->format('%y');
This returns me an int (Like 0 for < than a year, 2 for two years, etc.)
I need the result to be decimal as following:
0.9 - 9 months
1.2 - 1 year and two months
3.5 - 3 years and five months
and so on..
Thanks!
If you don't care about perfect accuracy:
return $interval->days / 365;
You could also do something like return $interval->y + $interval->m / 12 + $interval->d / 365.
Didn't even notice your weird decimal convention until I saw #2unco's comment. That would look like: return $interval->y . '.' . $interval->m.
Here you can see a function that does exactly that and with many options:
http://php.net/manual/es/function.date-diff.php#98615
<?php
/*
* A mathematical decimal difference between two informed dates
*
* Author: Sergio Abreu
* Website: http://sites.sitesbr.net
*
* Features:
* Automatic conversion on dates informed as string.
* Possibility of absolute values (always +) or relative (-/+)
*/
function s_datediff( $str_interval, $dt_menor, $dt_maior, $relative=false){
if( is_string( $dt_menor)) $dt_menor = date_create( $dt_menor);
if( is_string( $dt_maior)) $dt_maior = date_create( $dt_maior);
$diff = date_diff( $dt_menor, $dt_maior, ! $relative);
switch( $str_interval){
case "y":
$total = $diff->y + $diff->m / 12 + $diff->d / 365.25; break;
case "m":
$total= $diff->y * 12 + $diff->m + $diff->d/30 + $diff->h / 24;
break;
case "d":
$total = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h/24 + $diff->i / 60;
break;
case "h":
$total = ($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h + $diff->i/60;
break;
case "i":
$total = (($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i + $diff->s/60;
break;
case "s":
$total = ((($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i)*60 + $diff->s;
break;
}
if( $diff->invert)
return -1 * $total;
else return $total;
}
/* Enjoy and feedback me ;-) */
?>
Simpler and more accurate interval converter to days/hours/minutes/seconds:
function DateDiffInterval($sDate1, $sDate2, $sUnit='H') {
//subtract $sDate2-$sDate1 and return the difference in $sUnit (Days,Hours,Minutes,Seconds)
$nInterval = strtotime($sDate2) - strtotime($sDate1);
if ($sUnit=='D') { // days
$nInterval = $nInterval/60/60/24;
} else if ($sUnit=='H') { // hours
$nInterval = $nInterval/60/60;
} else if ($sUnit=='M') { // minutes
$nInterval = $nInterval/60;
} else if ($sUnit=='S') { // seconds
}
return $nInterval;
} //DateDiffInterval

convert time from database

I have a value in the database defined as mediumint(11). Currently it returns 3600 if I run a query.
This function here should convert that value to: 1 hr. But it doesn't when I run the function. I get no value.
function strTime($s) {
$d = intval($s/86400);
$s -= $d*86400;
$h = intval($s/3600);
$s -= $h*3600;
$m = intval($s/60);
$s -= $m*60;
if ($d) $str = $d . 'd ';
if ($h) $str .= $h . 'h ';
if ($m) $str .= $m . 'm ';
if ($s) $str .= $s . 's';
return $str;
}
Pretty basic, must be something with the value in the db?
Code that calls this (Joomla specific):
$query = "SELECT streaming_limit FROM #__cc_users WHERE user_id=".$user->id;
$db->setQuery($query);
$steaming_limit = $db->loadResult(); //returns 3600
echo strTime($streaming_limit); //returns nothing
A more robust way to do this is PHP's DateTime and DateInterval classes, which exist specifically for this sort of thing.
The code below creates two DateTime objects, adds the specified number of seconds
to one object and then calculates the interval between the two.
At that point you can format the time difference however you'd like using DateInterval::format
function strTimeDiff($seconds)
{
$date1 = new DateTime();
$date2 = new DateTime();
$date2->add(new DateInterval('PT'.$seconds.'S'));
$interval = $date1->diff($date2);
echo $interval->format('%d days, %h hours, %i minutes, %s seconds');
}
$secs_from_db = 3600;
echo strTimeDiff($secs_from_db);
// 0 days, 1 hours, 0 minutes, 0 seconds
You could try using something like this:
"converts number of seconds to an array containing hours, minutes and seconds separately"
From http://codeaid.net/php/convert-seconds-to-hours-minutes-and-seconds-%28php%29
<?php
/**
* Convert number of seconds into hours, minutes and seconds
* and return an array containing those values
*
* #param integer $seconds Number of seconds to parse
* #return array
*/
function secondsToTime($seconds)
{
// extract hours
$hours = floor($seconds / (60 * 60));
// extract minutes
$divisor_for_minutes = $seconds % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
// return the final array
$obj = array(
"h" => (int) $hours,
"m" => (int) $minutes,
"s" => (int) $seconds,
);
return $obj;
}

Categories