Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

convert to time (ebay time left)

P2DT16H2M40S
P20DT6H2M4S

P-> I dont know
DT->Days
H->Hours
M->Minutes
S->Seconds
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

http://www.mrkent.com/ebaytools/index.php
Look at the source code, it is written in javascript, if you need to convert it in php just say so.
Avatar of kaufmed
You could use my suggestion from your last question, just change the words accordingly within the regex pattern.
exactly as in the question before... use regex and get the things out that you are looking for...
Avatar of rgb192

ASKER

I cant find the source code on that page
I guess I'd have to go with kaufmed's suggestion - use regex to convert.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Blast!  Overruled again    ; )
I think we guessed right.  See "duration" on this page:
http://developer.ebay.com/devzone/merchandising/docs/callref/types/simpleTypes.html

Here is a slightly more comprehensive version, packaged as a function.  It does not cover the PnW or the Pdate formats, nor does it account for goofy things like fractional periods.
<?php // RAY_duration_strtotime.php
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');


// WHAT DOES THIS STRING MEAN?
$str = 'P1Y3M2DT16H2M40S';

// IT IS THE ISO8601 FORMAT FOR A DURATION.
// SEE http://en.wikipedia.org/wiki/ISO_8601

// IT MEANS THIS:
// P   = Pending (INDICATOR OF ISO8601 DURATION)
// 1Y  = 1 Year
// 2M  = 2 Months
// 2D  = Two Days
// T   = Time Separator (M is ambiguous)
// 16H = Sixteen hours
// 2M  = Two minutes
// 40S = Forty seconds



// STRTOTIME CANNOT HANDLE ISO-8601 DURATIONS
$dur = strtotime($str);
var_dump($dur);
echo "<br/>";

// GET THE DURATION IN SECONDS FOR USE IN ARITHMETIC
$dur = duration_strtotime($str);
echo "<br/>" . number_format($dur) . ' SECONDS';

// USE THE DURATION IN ARITHMETIC
echo "<br/>" . date('r', strtotime("NOW + $dur SECONDS"));



// A FUNCTION TO MAKE A DURATION STRING INTO A NUMBER OF SECONDS
function duration_strtotime($str, $sho=FALSE)
{
    // REMOVE THE AMBIGUITY OF MONTH AND MINUTE -- MAKE MONTH = X
    $arr = explode('T', $str);
    $arr[0] = str_replace('M', 'X', $arr[0]);
    $new = implode('T', $arr);

    // EXPAND THE STRING INTO SOMETHING SENSIBLE
    $new = str_replace('S', 'seconds ', $new);
    $new = str_replace('M', 'minutes ', $new);
    $new = str_replace('H', 'hours ',   $new);
    $new = str_replace('T', ' ',        $new);
    $new = str_replace('D', 'days',     $new);
    $new = str_replace('X', 'months ',  $new);
    $new = str_replace('Y', 'years ',   $new);
    $new = str_replace('P', NULL,       $new);

    if ($sho) var_dump($new);

    // RETURN THE NUMBER OF SECONDS IN THE DURATION
    return strtotime($new) - strtotime('NOW');
}

Open in new window

Avatar of rgb192

ASKER

Best answer, thank you
Thanks for the points.  I had never seen the "duration" format before.  Very interesting question, ~Ray