views:

38

answers:

1

I'm using PHP to get XML from an API (Say that 3 times fast). I'm getting all of the correct strings returned from all of the data. The problem comes when I try to reformat the time string with the following two methods:

$dataset = $xmlDoc->getElementsByTagName( "Class" );

foreach( $dataset as $row ) {

    $xmlStartTimes = $row->getElementsByTagName( "StartTime" );
    $xmlStartTime = $xmlStartTimes->item(0)->nodeValue;

    echo $xmlStartTime; // Gives eg. 12/30/1899 6:15:00 PM

    // Convert Time to 24H

    // Attempt 1:

    $cleanStartTime = preg_match('^12/30/1899 (0[1-9]|1[0-2]):(0[1-9]|[1-5][0-9]):([00]) (AM|PM|pm|am)$', $xmlStartTime, $matches);

    print_r($matches); // Gives empty array

    // Attempt 2:

    $cleanStartTime = date("H:i", strtotime("$xmlStartTime"));

    echo $cleanStartTime // Gives 1:00

}

Thanks for taking a look.

+1  A: 

Those times are outside UNIX timestamps range (32-bit). Use the DateTime extension instead:

<?php
$a = new DateTime("12/30/1899 6:15:00 PM");
echo $a->format("H:i");

gives 18:15.

Artefacto
`print date("H:i", strtotime('12/30/1899 6:15:00 PM'));` Shows 18:15, still good advice nonetheless.
Mike B
Great, I thought the date being given from the API might have caused strange effects.
Ben
@Mike B Not on platforms with 32-bit int's. On windows, your line gives me `01:00`.
Artefacto
@Artefacto Good catch :)
Mike B