Solved

convert to time

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


4days2hours5minutes3seconds


4days20hours50minutes30seconds


I want to convert this to time

0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 110

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 110

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 110

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 110

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

733 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