3

My Timestamp To String Function

// Timestamp To String
function time2string($time) {

    // DAYS
    $d = floor($time/86400);
    if ($d > 0) { $_d = $d.($d > 1 ? ' days' : ' day'); }
    else { $_d = ""; }

    // HOURS
    $h = floor(($time-$d*86400)/3600);
    if ($h > 0) { $_h = $h.($h > 1 ? ' hours' : ' hour'); }
    else { $_h = ""; }

    // MINUTES
    $m = floor(($time-($d*86400+$h*3600))/60);
    if ($m > 0) { $_m = $m.($m > 1 ? ' minutes' : ' minute'); }
    else { $_m = ""; }

    // SECONDS
    $s = $time-($d*86400+$h*3600+$m*60);
    if ($s >0) { $_s = $s.($s > 1 ? ' seconds' : ' second'); }
    else { $s = ""; }

    $time_str = $_d.' '.$_h.' '.$_m.' '.$_s;
    return $time_str;
}

Live Demo: https://eval.in/826278

Usage

time2string(22500)

6 hours 15 minutes

Desired Output

  • 1 second
  • 1 minute and 1 second
  • 1 hour, 1 minute and 1 second
  • 1 day, 1 hour, 1 minute and 1 second
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Tyler
  • 854
  • 1
  • 10
  • 26
  • @MagnusEriksson I do actually ask how to add the **and** feature to my script along with the commas, so no, not directly asking if I have done this the _best_ way – Tyler Jul 03 '17 at 06:01
  • @MagnusEriksson I'm not sure how I would achieve this without _a lot_ of `IF` statements making this rather small function a very large one. Do you have any tips to get myself going maybe? Thank you – Tyler Jul 03 '17 at 06:05
  • This code doesn't seem to work for me. https://eval.in/826236 . – Hanky Panky Jul 03 '17 at 06:05
  • 1
    You're setting `$d` but are using `$_d` (the same goes for `$m` and `$h` as well). This is just throwing _"Undefined variables"_. – M. Eriksson Jul 03 '17 at 06:08
  • follow this: https://stackoverflow.com/questions/44771808/display-most-recent-comments-by-date-and-time-in-php/44771856#44771856 – Siddhartha esunuri Jul 03 '17 at 06:11
  • Updating now, sorry, realise this is an older version! – Tyler Jul 03 '17 at 06:12
  • I've had to revert back to my previous version due to an error I couldn't find where it seemed like only one `IF` statement was being executed. Live demo here: https://eval.in/826260 – Tyler Jul 03 '17 at 06:21
  • Whats with the <10 thing? – Hanky Panky Jul 03 '17 at 06:31

2 Answers2

3

I've made myself a function to do this once, so I can reuse it for more applications:

function smart_implode($values, $join, $joinLast) {
    $lastValue = end($values); // take last value
    unset($arr[count($values)-1]); // remove from array
    $values = implode($join, $values); // implode all remaining values
    return $values.$joinLast.$lastValue; // and add the last value
}

echo smartImplode($timeValueArray, ", ", " and ");

This has the added bonus, that if you dont want to display 0 values (like 0 minutes), you're not stuck to a hardcoded solution. Just don't enter it in the smart_implode()

5 hours, 0 mins and 7 seconds -> 5 hours and 7 seconds 

Quick example for your specific code.

Martijn
  • 15,791
  • 4
  • 36
  • 68
  • I'm a little confused, does this work alongside the function I have made as otherwise I cannot see how you have added the "hours", "mins"... Also, if not, it doesn't seem to work here: https://eval.in/826326 -- Unless I am using this wrong? – Tyler Jul 03 '17 at 08:22
  • Yes, you use it (simplefied) like this: `smartImplode(array('5 hours', '7mins', '8seconds'))`; Your current code sets everything in a string, that needs to be as an array. – Martijn Jul 03 '17 at 08:23
  • You can use this function in your function to still return a string. – Martijn Jul 03 '17 at 08:23
  • 1
    I have edited your answer so that it is more in depth. I have also fixed the `smart_implode` function as it was throwing an undefined variable error. – JustCarty Jul 03 '17 at 08:34
  • I've taken your error fix, but stuck to the less detailed answer. Those often work better. And I've also added a link to a quick example, no need to add it completely – Martijn Jul 03 '17 at 09:01
  • @Martijn I am still a little stuck, could you kindly aid further? Here is my attempt so far; https://eval.in/826848 – Tyler Jul 03 '17 at 21:43
  • At the bottom of my post, there's an example – Martijn Jul 04 '17 at 07:05
3

I completely changed gears from my first post. I wanted to get away from all of the mathematical handling, so I decided to use DateTime objects... after all diff() is perfect for this task.

Code: (Demo)

function time2string($time) {
    if($time==0){return '0 seconds';}
    $t1=new DateTime();
    $t2=new DateTime("+$time seconds");
    $diff=$t1->diff($t2);
    $units=['days'=>'day','h'=>'hour','i'=>'minute','s'=>'second'];  // nominate units
    foreach($units as $k=>$v){
        if($diff->$k!=0){$result[]=$diff->$k.' '.$v.($diff->$k>1?'s':'');}  // store non-zero strings
    }
    if(sizeof($result)==1){
        return $result[0];  // return single element
    }else{
        $last=array_splice($result,-1);  // remove last element from $a and store separately
        return implode(', ',$result)." and {$last[0]}";  // return with correct delimiters
    }
}

echo time2string(122510); // output: 1 day, 10 hours, 1 minute and 50 seconds

echo time2string(0); // output: 0 seconds

echo time2string(1); // output: 1 second

echo time2string(9199800); // output: 106 days, 11 hours and 30 minutes

echo time2string(69206400); // output: 801 days

echo time2string(3599); // output: 59 minutes and 59 seconds
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • It does indeed, perfectly so. Sorry about the delay in awarding, I was playing around a little with it and got caught up in a minor none related bug! – Tyler Jul 04 '17 at 02:32