Re: date_diff

From: Date: Fri, 01 Aug 2008 09:59:08 +0000
Subject: Re: date_diff
References: 1 2 3 4 5 6 7  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969379223@lists.php.net to get a copy of this message
It does, thats why it works with date_add & date_sub but when it's var_dumped nothing is displayed just -> diff: object(DateInterval)#3 (0) { } -------------------------------------------------- From: "Richard Quadling" <rquadling@googlemail.com> Sent: Friday, August 01, 2008 10:53 AM To: <a_j_bellamy@hotmail.com> Cc: "PHP Documentation ML" <phpdoc@lists.php.net> Subject: Re: [PHP-DOC] date_diff
2008/7/31 <a_j_bellamy@hotmail.com>:
The DateInterval class needs to have a new method added to it to display the actual difference between the dates (2 days. 10 months. 3 years). Or date_diff returns a string e.g <?php echo date_diff($date, $other_date); ?> would output 3 days -------------------------------------------------- From: "Richard Quadling" <rquadling@googlemail.com> Sent: Thursday, July 31, 2008 5:41 PM To: <a_j_bellamy@hotmail.com> Cc: "PHP Documentation ML" <phpdoc@lists.php.net> Subject: Re: [PHP-DOC] date_diff
2008/7/31 <a_j_bellamy@hotmail.com>
Hi everybody! This might be the right mailing list and it might not be. I'm having a problem getting date_diff to work. I know date_diff accepts 2 DateTime objects and returns a DateInterval which I var_dump but it shows nothing Help is required! Regards Andy Heres my code: <?php $date = new DateTime("28-July-2008"); $other = new DateTime("31-July-2008"); $diff = date_diff($date, $other); echo '<br />date: '; var_dump($date); echo '<br /><br />other: '; var_dump($other); echo '<br /><br />diff: '; var_dump($diff); echo '<br /><br />get_class_methods - DateInterval : '; $class_methods = get_class_methods(new DateInterval('P2Y2M2D')); foreach ($class_methods as $method_name) {
      echo "$method_name(), ";
} echo '<br /><br />get_class_methods - DateTime : '; $class_methods = get_class_methods(new DateTime("31-July-2008")); foreach ($class_methods as $method_name) {
      echo "$method_name(), ";
} ?> Heres my output: date: object(DateTime)#1 (3) { ["date"]=> string(19) "2008-07-28 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London" } other: object(DateTime)#2 (3) { ["date"]=> string(19) "2008-07-31 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London" } diff: object(DateInterval)#3 (0) { } get_class_methods - DateInterval : __construct(), format(), createFromDateString(), get_class_methods - DateTime : __construct(), __wakeup(), __set_state(), createFromFormat(), getLastErrors(), format(), modify(), add(), sub(), getTimezone(), setTimezone(), getOffset(), setTime(), setDate(), setISODate(), setTimestamp(), getTimestamp(), diff(),
<?php $date = new DateTime("28-July-2008"); $other = new DateTime("31-July-2008"); $interval = date_diff($date, $other, True); date_add($date, $interval); var_dump($date); date_add($date, $interval); var_dump($date); outputs ... object(DateTime)#1 (3) { ["date"]=> string(19) "2008-07-31 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London" } object(DateTime)#1 (3) { ["date"]=> string(19) "2008-08-03 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London" } So, I can get the difference of 2 dates into an interval. I can then add that interval to other dates. I can't determine the value of the interval though. No interface for that. -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!"
I would have thought that the difference would be returned in the same format as you would supply when you wanted to make a dateinterval (i.e. P3D) maybe. -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!"


« previous php.doc (#969379223) next »