PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Date and Time » Date » Bug #335

Date::addSeconds() adds hours instead of seconds

Details

Submitted2003-12-02 02:31 UTC
Fromaashley at optimiser dot com
Assignedpajoye
StatusClosed
PackageDate
PHP Version4.3.3
OSGentoo Linux
Roadmaps(Not assigned)

Comments

[2003-12-02 02:31 UTC] aashley at optimiser dot com

Description:
------------
Date::addSeconds() incorrectly calls the creation of the of the Date_Span object so that the Span assumes that the number specified is in hours not seconds.

Patch to fix code:

--- /home/aashley/pear/Date/Date.php 2003-11-18 10:21:44.000000000 +0800
+++ /usr/share/pear/Date.php 2003-12-02 10:25:12.097214200 +0800
@@ -576,7 +576,7 @@
*/
function addSeconds($sec)
{
- $this->addSpan(new Date_Span($sec));
+ $this->addSpan(new Date_Span('00:00:'.$sec));
}

/**

Reproduce code:
---------------
<?php

$date = new Date('2003-01-01 00:00:00');

$date->addSeconds(10);

print $date->getDate();

?>

Expected result:
----------------
2003-01-01 00:00:10

Actual result:
--------------
2003-01-01 10:00:00

[2003-12-03 21:32 UTC] baba at php dot net

Thank you for taking the time to write to us, but this is not
a bug.

reproduce code produces expected result for me.
Date_Span code treates integer only paramater as seconds.
please check against 1.4rc1 package and comment if you can still produce this error.

[2003-12-04 02:11 UTC] aashley at optimiser dot com

Okay further investigation and I narrowed down the problem. The seconds value I was adding was being retireved from a database so was being passed to addSeconds as a string, which cases setFromString to be called which interperates the value as hours.

The following code demonstrates this:

<?php
include_once 'Date.php';
$date = new Date('2003-12-01 03:09:16');
$date->addSeconds('10');
print 'Add 10 seconds as a string: '.$date->getDate()."\n";
$date = new Date('2003-12-01 03:09:16');
$date->addSeconds(10);
print 'Add 10 seconds as an integer: '.$date->getDate()."\n";
?>

Patch to fix:

--- /home/aashley/pear/Date/Date.php 2003-12-04 10:11:51.978938200 +0800
+++ /usr/share/pear/Date.php 2003-12-04 10:10:34.500899754 +0800
@@ -582,7 +582,7 @@
*/
function addSeconds($sec)
{
- $this->addSpan(new Date_Span($sec));
+ $this->addSpan(new Date_Span((integer)$sec));
}

/**

[2003-12-15 08:22 UTC] aashley at optimiser dot com

if thats your intended behaviour for the function why have it at all? its just works as an alias for addSpan().

the fix I did was to make it behave as the name of the function implies its doing, adding this value as seconds.