Solved

convert to time

Posted on 2011-02-28
13
278 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 39

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
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.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

766 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