3

My question is, how with PHP we can have an automated system, which can do this: If we have $seconds= 120; And the script should get this value, see that this is equal to 2 min and then print this value in minutes. Same as we want to have it for days, lets say $days = 7; script needs to get this value, check the number of days and in this case it needs to print 1 week. Thank you guys!

gnur
  • 4,671
  • 2
  • 20
  • 33
Doolkin
  • 119
  • 3
  • 8

3 Answers3

8
<?php

    /**
     * @param int $secs
     *
     * @return string
     */
    function formatSeconds($secs) {
        $secs = (int)$secs;
        if ( $secs === 0 ) {
            return '0 secs';
        }
        // variables for holding values
        $mins  = 0;
        $hours = 0;
        $days  = 0;
        $weeks = 0;
        // calculations
        if ( $secs >= 60 ) {
            $mins = (int)($secs / 60);
            $secs = $secs % 60;
        }
        if ( $mins >= 60 ) {
            $hours = (int)($mins / 60);
            $mins = $mins % 60;
        }
        if ( $hours >= 24 ) {
            $days = (int)($hours / 24);
            $hours = $hours % 60;
        }
        if ( $days >= 7 ) {
            $weeks = (int)($days / 7);
            $days = $days % 7;
        }
        // format result
        $result = '';
        if ( $weeks ) {
            $result .= "{$weeks} week(s) ";
        }
        if ( $days ) {
            $result .= "{$days} day(s) ";
        }
        if ( $hours ) {
            $result .= "{$hours} hour(s) ";
        }
        if ( $mins ) {
            $result .= "{$mins} min(s) ";
        }
        if ( $secs ) {
            $result .= "{$secs} sec(s) ";
        }
        $result = rtrim($result);
        return $result;
    }

    echo formatSeconds(0), "\n";
    echo formatSeconds(30), "\n";
    echo formatSeconds(300), "\n";
    echo formatSeconds(3000), "\n";
    echo formatSeconds(30000), "\n";
    echo formatSeconds(300000), "\n";
    echo formatSeconds(3000000), "\n";
    echo formatSeconds(30000000), "\n";

Output:

0 secs
30 sec(s)
5 min(s)
50 min(s)
8 hour(s) 20 min(s)
3 day(s) 23 hour(s) 20 min(s)
4 week(s) 6 day(s) 53 hour(s) 20 min(s)
49 week(s) 4 day(s) 53 hour(s) 20 min(s)
binaryLV
  • 9,002
  • 2
  • 40
  • 42
  • `0 secs` is not really nice in english ;) – Shikiryu Feb 23 '11 at 09:52
  • @Shikiryu, would it be better if I wrote an answer in Latvian? – binaryLV Feb 23 '11 at 09:54
  • @binaryLV : no, but since you're answering in english... ;) 0 second means ... no second, therefor it doesn't need a `s` in sec. Otherwise, I upvoted your answer because it's good. – Shikiryu Feb 23 '11 at 10:03
  • @Shikiryu, http://en.wikipedia.org/wiki/Plural#Zero - "For example, in English, German, Dutch, Italian, Spanish and Portuguese, the plural form is used for zero or more than one, and the singular for one thing only." So it's "0 seconds", not "0 second". – binaryLV Feb 23 '11 at 11:00
  • @Shikiryu, and instead of "therefor" you should write "therefore" ;) – binaryLV Feb 23 '11 at 11:01
  • @binaryLV : Ouch for me but don't blame a typo ;) Didn't know that Plural / Zero rule. Pretty unlogical to me but still a rule and good to know! Thanks. – Shikiryu Feb 23 '11 at 11:10
  • 1
    Submitted a typo fix for ya. Hours was calculating mod 60 when it should be mod 24. (This causes the `53 hour (s)` in your output above. – Matt R. Oct 03 '12 at 16:39
5

4 week(s) 6 day(s) 53 hour(s) 20 min(s)

53 hour(s) ? also, 300000 seconds is 3 days 11hrs, not 3 days 23hrs

function formatSeconds($secs) {

    if (!$secs = (int)$secs)
        return '0 seconds';

    $units = array(
        'week' => 604800,
        'day' => 86400,
        'hour' => 3600,
        'minute' => 60,
        'second' => 1
    );

    $strs = array();

    foreach($units as $name=>$int){
        if($secs < $int)
            continue;
        $num = (int) ($secs / $int);
        $secs = $secs % $int;
        $strs[] = "$num $name".(($num == 1) ? '' : 's');
    }

    return implode(', ', $strs);
}

var_dump(formatSeconds(0));
var_dump(formatSeconds(30));
var_dump(formatSeconds(300));
var_dump(formatSeconds(3000));
var_dump(formatSeconds(30000));
var_dump(formatSeconds(300000));
var_dump(formatSeconds(3000000));
var_dump(formatSeconds(30000000));


string(9) "0 seconds"
string(10) "30 seconds"
string(9) "5 minutes"
string(10) "50 minutes"
string(19) "8 hours, 20 minutes"
string(28) "3 days, 11 hours, 20 minutes"
string(37) "4 weeks, 6 days, 17 hours, 20 minutes"
string(37) "49 weeks, 4 days, 5 hours, 20 minutes"
Felix Weelix
  • 352
  • 3
  • 7
1

You could use something as simple as:

$seconds = 120;
$minutes = floor($seconds/60);
echo $minutes.' min';

Am not really sure if I have understood your question properly though.

Alec Smart
  • 94,115
  • 39
  • 120
  • 184