Solved

convert to time (ebay time left)

Posted on 2011-03-01
10
920 Views
Last Modified: 2012-06-27
P2DT16H2M40S
P20DT6H2M4S

P-> I dont know
DT->Days
H->Hours
M->Minutes
S->Seconds
0
Comment
Question by:rgb192
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35007585
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35007629
You could use my suggestion from your last question, just change the words accordingly within the regex pattern.
0
 
LVL 5

Expert Comment

by:Kendor
ID: 35007678
exactly as in the question before... use regex and get the things out that you are looking for...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rgb192
ID: 35007685
I cant find the source code on that page
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35007780
I guess I'd have to go with kaufmed's suggestion - use regex to convert.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 35007797
Sure wish we could find a definition of what that field actually contains.  This seems intuitively correct.
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');


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


// GUESSING IT MEANS THIS:
// P = Pending
// 2D = Two Days
// T = Time
// 16H = Sixteen hours
// 2M = Two minutes
// 40S = Forty seconds


// TESTED HERE: http://www.mrkent.com/ebaytools/index.php
// RESULT DID NOT MAKE SENSE - IT WAS EXACTLY 24 HOURS IN THE FUTURE
// Your Ebay deadline of (P2DT16H2M40S):
// Wednesday, 03/02/2011 - 9:41 AM EST . . . !


// EXPAND THE STRING INTO SOMETHING SENSIBLE
$new = $str;
$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('P', 'now + ',   $new);

// PRODUCE A HUMAN-READABLE DATE
$dat = date('r', strtotime($new));

// CONVERT TO SECONDS
$sec = strtotime($new) - strtotime('now');

// SHOW THE WORK PRODUCTS
echo "<br/>$str";
echo "<br/>$new";
echo "<br/>$sec";
echo "<br/>$dat";

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35007948
Blast!  Overruled again    ; )
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35008681
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

0
 

Author Closing Comment

by:rgb192
ID: 35010475
Best answer, thank you
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35010750
Thanks for the points.  I had never seen the "duration" format before.  Very interesting question, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question