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

asked on

convert to time



4days2hours5minutes3seconds


4days20hours50minutes30seconds


I want to convert this to time

Avatar of Aaron Tomosky
Aaron Tomosky
Flag of United States of America image

Like now plus time? A datetime field can't just be a stopwatch.
To what format ?
have a look at regular expressions.
for example you could do something like this:

<?php
$returnValue = preg_split('/[a-zA-Z]+/', '4days20hours50minutes30seconds');
?>

you can access $returnValue - it is an array that contains your numbers (d, h, m, s)
now you can do whatever you want with those...

have a look at http://php.net/manual/de/function.time.php



Hai,

If this is the format that your getting

1) 4days2hours5minutes3seconds
2) 4days20hours50minutes30seconds

 - Then,you can just write a function to remove the letters from the string
 - Or else you know that the number of letters(count) after each number(numeric),so accordingly the use the PHP function to remove them all and have the numbers alone





If you can guarantee that you will always have that format, meaning it will always be "Xdays", for example, then you should be able to use regex for this. As an alternative to Kendor's example, this would allow the phrases to be in any order, or even some to be left out entirely. Each variable should be an array, so you would take the first index of each resulting array--I am merely dumping them below. This is untested, but I believe it should work. I can test when I get to work and have access to my php machine  = )
<?php

    $source = "4days20hours50minutes30seconds";

    preg_match("/\d+(?=days?)/", $source, $days);
    preg_match("/\d+(?=hours?)/", $source, $hours);
    preg_match("/\d+(?=minutes?)/", $source, $minutes);
    preg_match("/\d+(?=seconds?)/", $source, $seconds);

    var_dump($days);
    var_dump($hours);
    var_dump($minutes);
    var_dump($seconds);
?>

Open in new window

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
@Ray_Paseur

Does that work even if the string doesn't have spaces? The examples in the manual show spaces between the number and the word.
@kaufmed: Yes, it worked for me. PHP 5.3+  Test it on my server in the link posted above.  Observed behavior is that strtotime() adds these sorts of strings to the assumed value of "now."
Here is another demonstration case:
http://www.laprbass.com/RAY_temp_rgb192.php
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');

// IN SECONDS, THIS IS 4*24*60*60 + 2*60*60 + 5*60 + 3
$str = '4days2hours5minutes3seconds';

// WHAT WE THINK StrToTime() SHOULD PRODUCE
$val = 4*24*60*60 + 2*60*60 + 5*60 + 3;

// GENERATE THE NUMBER OF SECONDS
$tsm = strtotime($str) - strtotime('now');

// SHOW THE WORK PRODUCTS
echo "<br/>$str";
echo "<br/>$val";
echo "<br/>$tsm";

Open in new window

Avatar of rgb192

ASKER

Thanks,

https://www.experts-exchange.com/questions/26855251/convert-to-time-ebay-time-left.html

I have a similar question
ebay gives time left in the format

P2DT16H2M40S
P20DT6H2M4S

P-> I dont know
DT->Days
H->Hours
M->Minutes
S->Seconds
@Ray_Paseur

Thx for confirming...  I was just curious.  You PHP knowledge is vast and powerful. I am not worthy to bask in its glow  = )
Ha!  Thanks for your kind words, kaufmed.  I'll look at the other eBay-related question in a moment.  Best to all, ~Ray