php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50075 Provide single value difference in DateInterval->diff()
Submitted: 2009-11-04 14:31 UTC Modified: 2022-05-13 10:31 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: RQuadling at GMail dot com Assigned:
Status: Wont fix Package: Date/time related
PHP Version: 5.3.1RC3 OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
15 - 6 = ?
Subscribe to this entry?

 
 [2009-11-04 14:31 UTC] RQuadling at GMail dot com
Description:
------------
The output of DateInterval->diff() currently shows the breakdown of 
the 
difference (y, m, d, h, i, s, days, invert).

Which is fine. But I'd like to be able to have the number of seconds 
difference.

The following patch adds a new entry to the output: diff.

It is a straight calculation of the seconds based upon days, h, i and 
s, 
and takes into consideration invert.

The patch below is the same for branch/5_3 and trunk.


I think this patch and the DateInterval object comparison patch 
reported in Bug#49914 by aharvey would be good additions.

Regards,

Richard.

Reproduce code:
---------------
Index: php_date.c
===================================================================
--- php_date.c	(revision 290198)
+++ php_date.c	(working copy)
@@ -2245,6 +2245,16 @@
 	PHP_DATE_INTERVAL_ADD_PROPERTY("s", s);
 	PHP_DATE_INTERVAL_ADD_PROPERTY("invert", invert);
 	PHP_DATE_INTERVAL_ADD_PROPERTY("days", days);
+	
+	MAKE_STD_ZVAL(zv);
+	ZVAL_LONG(zv, ((((((
+		intervalobj->diff->days * 24) + 
+		intervalobj->diff->h) * 60) + 
+		intervalobj->diff->i) * 60) + 
+		intervalobj->diff->s) * (
+		intervalobj->diff->invert ? -1 : 1)
+	);
+	zend_hash_update(props, "diff", 5, &zv, sizeof(zval), NULL);
 
 	return props;
 }



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-04 15:09 UTC] derick@php.net
This patch is not correct, because it doesn't take care of daylight savings time nor should "invert" being taken into account as it's already a seperate field. I wouldn't call it "diff" either, but "seconds" just like there is "days".
 [2009-11-04 15:43 UTC] RQuadling at GMail dot com
I'm not sure how DST is relevant here. DateInterval will already have worked out the difference in days, hours, mins, etc. All I'm providing is a total number of seconds.

I take on the issue with invert, but it does mean having to do some userland calcs.

diff -> seconds. Yep. Obviously.

Index: php_date.c
===================================================================
--- php_date.c	(revision 290198)
+++ php_date.c	(working copy)
@@ -2245,6 +2245,15 @@
 	PHP_DATE_INTERVAL_ADD_PROPERTY("s", s);
 	PHP_DATE_INTERVAL_ADD_PROPERTY("invert", invert);
 	PHP_DATE_INTERVAL_ADD_PROPERTY("days", days);
+	
+	MAKE_STD_ZVAL(zv);
+	ZVAL_LONG(zv, (((((
+		intervalobj->diff->days * 24) + 
+		intervalobj->diff->h) * 60) + 
+		intervalobj->diff->i) * 60) + 
+		intervalobj->diff->s
+	);
+	zend_hash_update(props, "seconds", 8, &zv, sizeof(zval), NULL);
 
 	return props;
 }
 [2009-11-04 15:56 UTC] derick@php.net
DST is relevant because the following will show "one day", but it is not 86400 seconds:

<?php
date_default_timezone_set( "Europe/London" );
$a = new DateTime( "2009-10-25 00:00" );
$b = new DateTime( "2009-10-26 00:00" );
$d = $a->diff( $b );
echo $d->format( '%d %h:%i:%s' ), "\n";
echo $b->format( 'U' ) - $a->format( 'U' ), "\n";
echo $a->format( DateTime::ISO8601 ), "\n";
echo $b->format( DateTime::ISO8601 ), "\n";
?>

 [2009-11-04 16:03 UTC] RQuadling at GMail dot com
Which would suggest to me that DateTime->diff() has a bug.

Personally, if I'm asking for the difference between 2 dates, the 
physical time between them would be what I would expect to be reported.

So, when checking over DST, the result could be 23 hours (GMT->BST) or 1 
day + 1 hour (BST -> GMT).

Neither are 1 day.
 [2010-11-24 10:30 UTC] jani@php.net
-Package: Feature/Change Request +Package: Date/time related
 [2022-05-13 10:31 UTC] derick@php.net
-Status: Open +Status: Wont fix
 [2022-05-13 10:31 UTC] derick@php.net
I am marking this as "Won't Fix". If this is still an issue, please open a new ticket at https://github.com/php/php-src/issues/
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 08:01:28 2024 UTC