Jump to content

Recommended Posts

Hi,

I have a string representing time left on an auction which is returned to me from the eBay API.

It reads like so:

 

P2T9H37M8S

 

I need to reformat this to read:

 

2 Days 9 Hours 37 Minutes

 

When a designation hits zero I need it to disappear. As in:

 

1 Day 2 Hours 10 Minutes

23 Hours 3 Minutes

1 Hours 2 Minutes

58 Minutes

And finally nothing at all appears.

 

I'm totally ignorant on how to approach this as I'm not a scripting guru.

 

Any help would be greatly appreciated :)

Link to comment
https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/
Share on other sites

A way:

<?php
$time = "P2T9H37M8S";
// days
$start = strpos($time, "P") + 1;
$end = strpos($time, "T");
$days = substr($time, $start, $end-$start);
// hours
$start = strpos($time, "T") + 1;
$end = strpos($time, "H");
$hours = substr($time, $start, $end-$start);
// minutes
$start = strpos($time, "H") + 1;
$end = strpos($time, "M");
$minutes = substr($time, $start, $end-$start);
// seconds
$start = strpos($time, "M") + 1;
$end = strpos($time, "S");
$seconds = substr($time, $start, $end-$start);

echo ($days=="0" && $hours=="0" && $minutes=="0" && $seconds=="0")
? "<p>Time's up!</p>" : "<p>$days days $hours hours $minutes minutes $seconds seconds</p>";
?>

Thanks so much for both replies!

 

Both work but need tweaking due to my own oversight.

 

The data returned by eBay represents time left on an auction. Thus it changes constantly.

 

As such, when there are no more days it reads as:

PT8H10M26S

When there are no more hours it reads like:

PTH10M26S

etc., etc. until it would read:

PTHMS

 

How can this be accommodated?

 

Thanks so much! Both are great solutions  ;)

Thanks sasa!

 

That seems to work well.

 

Now I have to add in styling which I think I can do.

I'll add to this post if I run into problems with that.

 

You used regex correct? I haven't a clue how it works but would like to at least know the mechanism you're using.

Can you post a brief explanation?

 

Thanks again to you and to charlieholder :)

What is the significance of the *.* in *$dare .=* ?

 

The dot ( . ) is the concatenation operator, so if you put it before the assignment operator then the right operand will be added to the left operand (not added in the arithmetic sense, but by sticking things together).

'/P(\d*)T(\d*)H(\d*)M/'

/    start pattren

P    character 'P'

(    start of 1st subpattern

\d  any digit

*    zero or more times (+ means one or more times)

)    end of 1st subpattern

T    char 'T'

(    start of 2nd subpattern

\d  any digit

*    zero or more times (+ means one or more times)

)    end of 2nd subpattern

etc.

/    end pattern

Thanks Daniel0!

Please pardon my ignorance, but why is he doing that?

 

Why not $dare= ?

 

Here is a more visual example. Perhaps that'll help with the understanding :)

 

<?php
$var1 = 'abc';
$var2 = 'def';

$var1 .= $var2; // $var1 now contains: abcdef

// this is the same as:
// $var1 = $var1 . $var2;

//---
$var1 = 'abc';
$var2 = 'def';

$var1 = $var2; // $var1 now contains: def
?>

 

You can do that with any operator, e.g. $num += 5 (add 5 to $num (the same as $num = $num + 5)).

Thanks sasa,

The only thing I'm confused about is where to enclose the output in a div.

 

Should I concatenate it in this string?

$dare .= $d[$i] ? $d[$i].' '.$x[$i].($d[$i] > 1 ? 's ' : ' ') :'';

 

If so, how?

 

It does print properly BTW but I just need format it.

Once I understand the concatenation I'll be fine.

 

Thanks!

Update,

Got the div formatting and styling to work.

Wrapped $dare in it.

echo '<div style="text-align:center;clear:both;background:#cccccc;padding:2px;"><em>Time Left: </em>' . $dare . '   ';

 

Works like a charm!

 

Many, many thanks to all who assisted. I got a good education on this one  :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.