|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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;
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 09:00:02 2025 UTC |
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; }