Solved

convert to time

Posted on 2011-02-28
13
276 Views
Last Modified: 2012-06-27


4days2hours5minutes3seconds


4days20hours50minutes30seconds


I want to convert this to time

0
Comment
Question by:rgb192
  • 4
  • 3
  • 2
  • +4
13 Comments
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 35004036
Like now plus time? A datetime field can't just be a stopwatch.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35004668
To what format ?
0
 
LVL 5

Expert Comment

by:Kendor
ID: 35004782
have a look at regular expressions.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Expert Comment

by:Kendor
ID: 35004879
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



0
 
LVL 7

Expert Comment

by:Vimal DM
ID: 35005596
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





0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35006159
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

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 35006241
This is really pretty easy.  Just use strtotime(), like this:
http://www.laprbass.com/RAY_strtotime.php?s=4days20hours50minutes30seconds

I have an article here at EE that teaches how to handle DATETIME strings.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

HTH, ~Ray
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35006252
@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.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 35007388
@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."
0
 
LVL 109

Expert Comment

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

0
 

Author Closing Comment

by:rgb192
ID: 35007512
Thanks,

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26855251.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
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35007524
@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  = )
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 35007611
Ha!  Thanks for your kind words, kaufmed.  I'll look at the other eBay-related question in a moment.  Best to all, ~Ray
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to count occurrences of each item in an array.

770 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