Menu

#28 Fix parse_w3cdtf()

open
nobody
None
5
2006-02-17
2006-02-17
Anonymous
No

Array
(
[0] => 2006-02-17T19:45:28+09:00
[1] => 2006
[2] => 02
[3] => 17
[4] => 19
[5] => 45
[6] => :28
[7] => 28
[8] => +
[9] => 09
[10] => 00
)

Discussion

  • Nobody/Anonymous

    Patch for rss_utils.inc

     
  • Viego

    Viego - 2006-05-03

    Logged In: YES
    user_id=1239697

    What happens if there is no second in the timestamp. Is
    position 6 and 7 empty or will the array be only from 0 to 8?

    I think the 2nd case is the right one and so the patch
    should be changed a bit.

     
  • Nobody/Anonymous

    Logged In: NO

    What? Theres not enough info to declaire a bug in this post. Please post more info.

     
  • Jacob F.

    Jacob F. - 2007-07-06

    Logged In: YES
    user_id=1386245
    Originator: NO

    The problem is not that seconds might end up being index 7, the problem is in the regex in the preg_match. The colon (:) between minutes and seconds is being included with the seconds value, this is also why PHP 5 throws a strict warning about expecting a long and getting a string, because it is receiving :28 instead of just 28.

    $pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";

    should be:

    $pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):((\d{2}))?(:?([-+])(\d{2}):?(\d{2})|(Z))?/";

     
  • Nobody/Anonymous

    Logged In: NO

    Para arreglar el problema hay que recoger el elemento 7 del array $match en vez del elemento 6.

    Con esto solucionamos el bug, y funciona perfectamente para fechas con y sin segundos, ya que devuelve cadena vacía cuando no hay segundos.

    Vamos a flipar para convertir aplicaciones de PHP4 a PHP5 con problemas de este tipo que no sabes de donde te vienen :P

     
  • Nobody/Anonymous

    Logged In: NO

    You just need a ?: before the seconds bit of the regexp.

     
  • Karl DeBisschop

    Karl DeBisschop - 2009-08-01

    the function validates the format and then determines the epoch. Why not simplify the regexp to validate, then simply use strtotime to find the epoch?

    function parse_w3cdtf ( $date_str ) {

    # regex to match wc3dtf
    $pat = "/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?([-+]\d{2}:?\d{2}|Z)?/";
    if ( preg_match( $pat, $date_str ) ) {
    return strtotime( $date_str );
    }
    else {
    return -1;
    }
    }

     

Log in to post a comment.