Calculate total seconds in PHP DateInterval
Could you not compare the time stamps instead?
$now = new DateTime('now');
$diff = $date->getTimestamp() - $now->getTimestamp()
Convert DateInterval object to seconds in php
There is a function format
for this. But it wont return the number of seconds. To get number of seconds you use this technique
$seconds = abs($datetime1->getTimestamp()-$datetime2->getTimestamp());
If you really want to use $interval
you need to calculate it.
$seconds = $interval->days*86400 + $interval->h*3600
+ $interval->i*60 + $interval->s;
Here
86400
is the number of seconds in a day3600
is the number of seconds in an hour60
is the number of seconds in a minute
How to convert Php::DateInterval to seconds?
You could access the public properties of the DateInterval
class and do some math on it:
$seconds = $d->s + ($d->i * 60) + ($d->h * 3600) + ($d->d * 86400) + ($d->m * 2592000); // and so on
but once we get into the months there will be variance by +/- 2 days unless we stick to an arbitrary definition of a month as 2592000 secs
(30 days).
You can also use the difference of the two UNIX timestamps from the DateTime objects (but you will end up having problems with the dates being less than year of 1970):
$seconds = $end->getTimestamp() - $start->getTimestamp();
Get interval seconds between two datetime in PHP?
You can use strtotime() to do that:
$diff = strtotime('2009-10-05 18:11:08') - strtotime('2009-10-05 18:07:13')
A similar approach is possible with DateTime objects, e.g.
$date = new DateTime( '2009-10-05 18:07:13' );
$date2 = new DateTime( '2009-10-05 18:11:08' );
$diff = $date2->getTimestamp() - $date->getTimestamp();
Formatting DateInterval with huge seconds value
This can be achieved with DateTime class
Use:
echo secondsToTime(1640467);
18 days, 23 hours, 41 minutes and 7 seconds
Function:
function secondsToTime($seconds) {
$dtF = new DateTime("@0");
$dtT = new DateTime("@$seconds");
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
sample demo
PHP DateInterval create split second (microsecond or milisecond) interval
There is a way to do this using DateInterval::createFromDateString
:
$di = DateInterval::createFromDateString('123456 microseconds');
var_dump($di);
Output:
object(DateInterval)#1 (16) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(0)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["f"]=>
float(0.123456)
["weekday"]=>
int(0)
["weekday_behavior"]=>
int(0)
["first_last_day_of"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
bool(false)
["special_type"]=>
int(0)
["special_amount"]=>
int(0)
["have_weekday_relative"]=>
int(0)
["have_special_relative"]=>
int(0)
}
Demo on 3v4l.org
From the manual:
time
A date with relative parts. Specifically, the relative formats supported by the parser used for
strtotime()
andDateTime
will be used to construct the DateInterval.
Covert time duration format %h%m%s to seconds
Your "1h1m1s" format looks pretty compatible with PHP's DateInterval
class. You could construct one and then use this answer to calculate the seconds.
For example, you just need to uppercase the letters in "1h1m1s", then prefix it with "PT" to make it an interval spec.
/**
* From https://stackoverflow.com/a/25149337/283366, please upvote it
* @param DateInterval $dateInterval
* @return int seconds
*/
function dateIntervalToSeconds($dateInterval)
{
$reference = new DateTimeImmutable;
$endTime = $reference->add($dateInterval);
return $endTime->getTimestamp() - $reference->getTimestamp();
}
$interval = new DateInterval('PT' . strtoupper($duration));
$seconds = dateIntervalToSeconds($interval);
Demo ~ https://3v4l.org/UaEYB
get php DateInterval in total 'minutes'
abs((new \DateTime("48 hours"))->getTimestamp() - (new \DateTime)->getTimestamp()) / 60
That's the easiest way to get the difference in minutes between two DateTime
instances.
Related Topics
Performance of Static Methods VS. Functions
Category Hierarchy (Php/Mysql)
How to Use Multi Auth in Laravel 5.2
Turn Off Deprecated Errors in PHP 5.3
How to Add Private Github Repository as Composer Dependency
Codeigniter Check for User Session in Every Controller
Get Div Content from External Website
Calculate Total Seconds in PHP Dateinterval
How to Echo the Whole Content of a .HTML File in PHP
PHP Generated Xml Shows Invalid Char Value 27 Message
How to Create a Twig Custom Tag That Executes a Callback
Laravel Mail: Pass String Instead of View
Non-Static Method ..... Should Not Be Called Statically
Detecting Ajax in PHP and Making Sure Request Was from My Own Website
S3 Direct Upload Restricting File Size and Type
Why PHP Iteration by Reference Returns a Duplicate Last Record