Solved

convert to time (ebay time left)

Posted on 2011-03-01
10
903 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 109

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 109

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 109

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
two ways encryption with php 3 37
How to fix Datetime in MySQL? 4 48
XML extra information 8 29
simple html dom php accessing table/cell  values 4 20
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 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