Solved

convert to time

Posted on 2011-02-28
13
274 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
 
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 74

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

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 74

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 108

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 108

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 74

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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now