Project

General

Profile

Actions

Bug #30931

closed

date() expects parameter 2 to be long, string given in class.tslib_content.php

Added by Simon Schaufelberger over 12 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Must have
Assignee:
-
Category:
Frontend
Target version:
-
Start date:
2011-10-14
Due date:
% Done:

100%

Estimated time:
TYPO3 Version:
4.6
PHP Version:
5.2
Tags:
Complexity:
Is Regression:
Sprint Focus:

Description

I have the following typoscript (part of a sql string)

20 = TEXT
20.date = U
20.noTrimWrap = | AND date > ||

and i get the following warning message:

Warning: date() expects parameter 2 to be long, string given in /typo3/sysext/cms/tslib/class.tslib_content.php on line 2529

content should NEVER be a string by default here, it as written in the php docs:

string gmdate ( string $format [, int $timestamp = time() ] )

it shold be an int.

public function stdWrap_date($content = '', $conf = array()) {
    $content = ($conf['date.']['GMT'] ? gmdate($conf['date'], $content) : date($conf['date'], $content));
    return $content;
}

for me the code should be something like:

public function stdWrap_date($content = '', $conf = array()) {
    if ($content !== '') {
        $content = ($conf['date.']['GMT'] ? gmdate($conf['date'], $content) : date($conf['date'], $content));
    } else {
        $content = ($conf['date.']['GMT'] ? gmdate($conf['date']) : date($conf['date']));
    }
    return $content;
}

Related issues 2 (0 open2 closed)

Related to TYPO3 Core - Bug #38717: strftime() expects parameter 2 to be long, string givenClosedJigal van Hemert2012-07-06

Actions
Has duplicate TYPO3 Core - Bug #24276: PHP Warning may occur in backend UI with recent PHP versionClosed2010-12-02

Actions
Actions #1

Updated by Simon Schaufelberger over 12 years ago

a workaround would be:

20.value = UNIX_TIMESTAMP()

and remove the date line.

Actions #2

Updated by Markus Klein over 12 years ago

What is your intention about assigning U to 20.date?

The documentation says:
The content should be data-type "UNIX-time".

I do agree to change the behavior to prevent this warning.
Can you push a patch to gerrit?

Actions #3

Updated by Simon Schaufelberger over 12 years ago

My intention is to get the current timestamp which is only possible in typo3 with typoscript like that.

No behaviour needs to be changed, just the error handling... (if no "parameter"/$content is given)

Can you push a patch to gerrit?

yes, sure.

Actions #4

Updated by Mr. Hudson over 12 years ago

  • Status changed from New to Under Review

Patch set 1 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

Actions #5

Updated by Mr. Hudson over 12 years ago

Patch set 2 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

Actions #6

Updated by Mr. Hudson over 12 years ago

Patch set 3 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

Actions #7

Updated by Jigal van Hemert over 12 years ago

date() and gmdate() convert a big range of values to an integer:

integers : OK
floats : OK
NULL : OK ( => zero)
hex / decimal direct values : OK (like in date( 'U', 0xFF); )
strings:
- whitespace at beginning allowed (' 1234')
- trailing non-numeric garbage is ignored ( '1234abc' => 1234, but also '0xFFyyyy' => 255)
- +/- signs at beginning allowed ('+0' => 0

Next version will use a regular expression to check string values for validity.

Actions #8

Updated by Mr. Hudson over 12 years ago

Patch set 4 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

Actions #9

Updated by Simon Schaufelberger over 12 years ago

@Jigal van Hemert: did/could you write some unit tests for that?

Actions #10

Updated by Markus Klein over 12 years ago

Jigal, I'm sure you're referencing to this:
www.php.net/manual/en/language.types.integer.php

But:
  • I can't find any hint that date() is accepting floats.
  • Where does it say that leading white spaces are trimmed automatically?

I do believe a simple intval() or is_numeric() check would suffice.

Actions #11

Updated by Jigal van Hemert over 12 years ago

I checked the internal source code of PHP where it uses a function to 'interpret' a parameter to a long (integer) value. This function is different than intval() or is_numeric() uses. For example date('U', '0xFFyyy') = 255 while intval('0xFFyyy') = 0 and is_numeric('0xFFyyy') = FALSE.

The problem with this issue is that we have to make sure that things that worked before will still work (especially for 4.5 and 4.4 branches), while getting rid of the ugly warnings.

Below a small test script to see what date() accepts and that the regexp matches this (at least for strings).

Unit tests would be very nice indeed.

<?php
$date = array('-2text1',0,'-0', 12345678, '0020', '0xFFyyy', '-0xEEyyy', '-1.0e4', '   12345.6', '12345,7', 12345.6, NULL, TRUE, ' +    1234567abc', 'plaap', '');
foreach ($date as $tryDate) {
    echo 'tryDate: ';
    var_export($tryDate);
    echo "<br />\nis_numeric: ";
    var_export(is_numeric($tryDate));
    echo "<br />\nintval: ";
    var_export(intval($tryDate));
    echo "<br />\nusing testAgainstRegexp: ";
    var_export(testAgainstRegexp($tryDate));

    echo "<br />\ndate('U' : ";
    $plaap = date('U', $tryDate);
    var_export($plaap);
    echo "<br />\ngmdate('U' : ";
    $plaap = gmdate('U', $tryDate);
    var_export($plaap);
    echo "<br />\n";
    echo "--------------------<br />\n";
}

    function testAgainstRegexp ($value) {
        return preg_match('/^[[:space:]]* #allow whitespace at beginning
                    (
                        [+-]?(\d+)(?![xX]) #(un)signed decimal which is not the start of a hexadecimal number
                        |
                        0[xX][0-9a-fA-F]+ #unsigned hexadecimal number
                        |
                        [+-]?((\d+|((\d*\.\d+)|(\d+\.\d*)))[eE][+-]?\d+) #floating point notation
                    )
                    /x', $value);    
    }
?>
Actions #12

Updated by Dmitry Dulepov over 12 years ago

Do we really need that complexity? Why don't we simply do intval? What is the use case for having 0x23427235 as a string parameter?

I'd say go for a simple integer for now and expand that in future if there is a real use case for this. There is no need for complicating this more than necessary.

Actions #13

Updated by Mr. Hudson over 12 years ago

Patch set 5 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

Actions #14

Updated by Mr. Hudson over 12 years ago

Patch set 6 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

Actions #15

Updated by Mr. Hudson over 12 years ago

Patch set 7 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

Actions #16

Updated by Simon Schaufelberger over 12 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100
Actions #17

Updated by Benni Mack over 5 years ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF