Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:06:22+00:00 2026-06-17T16:06:22+00:00

I have build a calculating script that takes seconds and show them in y:d:h:m:s

  • 0

I have build a calculating script that takes seconds and show them in y:d:h:m:s containing labels etc.

eg. 15768012 = 1 year 12 seconds and 27362 = 15 hours 6 minutes 2 seconds.

The script works perfectly, but I would like to look in the string and split it by the last number (not the first) and then insert the word and so that:

15768012 = 1 year AND 12 seconds and 27362 = 15 hours 6 minutes AND 2 seconds.

I can write my script if you need it, but I don’t think it will help you solve this question..

let’s say that $string = '15 hours 6 minutes 2 seconds' how would you split it and import the text and ?

<?
if (isset($_POST['number'])) {
    $x = $_POST['number'];
} else {
    $x = 54098;
}

// Labels
$lb_y = 'year';
$lb_ys = 'years';
$lb_d = 'day';
$lb_ds = 'days';
$lb_h = 'hour';
$lb_hs = 'hours';
$lb_m = 'minute';
$lb_ms = 'minutes';
$lb_s = 'second';
$lb_ss = 'seconds';
$lb_and = 'and';

//$x = 54098; // Time in seconds - change to $row['time'];

$f = 15768000; // seconds in a year
$d = 43200; // seconds in a day
$t = 3600; // seconds in an hour
$m = 60; // senconds in a minute


// Let's check if it is more than a minute
if ($x >= $m) {

    // More than a minute
    // Let's check if it is more than an hour
    if ($x >= $t) {

        // More than an hour
        // Let's check if it is more than a day
        if($x >= $d) {

            // More than a day
            // Let's check if it is more than a year
            if ($x >= $f) {

                // More than a year
                // Calculate years
                $a = $x / $f;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($a, '.'))) {
                    $a = substr($a, 0, $cal);
                }

                // $k = what's left
                $k = $x - ($f * $a);

                // Calculate days
                $b = $k / $d;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($b, '.'))) {
                    $b = substr($b, 0, $cal);
                }

                // $y = what's left
                $y = $k - ($d * $b);

                // Calculate hours
                $c = $y / $t;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($c, '.'))) {
                    $c = substr($c, 0, $cal);
                }

                // $z = what's left
                $z = $y - ($t * $c);

                // Calculate minutes
                $e = $z / $m;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($e, '.'))) {
                    $e = substr($e, 0, $cal);
                }

                // $q = what's left
                $q = $z - ($m * $e);

                // Rewrite numbers if below 9
                if ($a <= 9) { $xa = '0'.$a; } else { $xa = $a; }
                if ($b <= 9) { $xb = '0'.$b; } else { $xb = $b; }
                if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
                if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
                if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }

                // Rewrite labels
                if ($a <= 1) { $lb_ys = $lb_y; }
                if ($b <= 1) { $lb_ds = $lb_d; }
                if ($c <= 1) { $lb_hs = $lb_h; }
                if ($e <= 1) { $lb_ms = $lb_m; }
                if ($q <= 1) { $lb_ss = $lb_s; }

                // if == 0 - do not show
                $a = $a.' '.$lb_ys.' ';
                if ($b == 0) {$b = '';} else {$b = $b.' '.$lb_ds.' ';}
                if ($c == 0) {$c = '';} else {$c = $c.' '.$lb_hs.' ';}
                if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms.' ';}
                if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}

                echo $xa.':'.$xb.':'.$xc.':'.$xe.':'.$xq;
                echo '<br>'.$a.$b.$c.$e.$q;


            } else {

                // Less than a year - but more than one day
                // Calculate days
                $b = $x / $d;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($b, '.'))) {
                    $b = substr($b, 0, $cal);
                }

                // $y = what's left
                $y = $x - ($d * $b);

                // Calculate hours
                $c = $y / $t;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($c, '.'))) {
                    $c = substr($c, 0, $cal);
                }

                // $z = what's left
                $z = $y - ($t * $c);

                // Calculate minutes
                $e = $z / $m;

                // Get everything before the separator '.'
                if (false !== ($cal = strpos($e, '.'))) {
                    $e = substr($e, 0, $cal);
                }

                // $q = what's left
                $q = $z - ($m * $e);

                // Rewrite numbers if below 9
                if ($b <= 9) { $xb = '0'.$b; } else { $xb = $b; }
                if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
                if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
                if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }

                // Rewrite labels
                if ($b <= 1) { $lb_ds = $lb_d; }
                if ($c <= 1) { $lb_hs = $lb_h; }
                if ($e <= 1) { $lb_ms = $lb_m; }
                if ($q <= 1) { $lb_ss = $lb_s; }

                // if == 0 - do not show
                $b = $b.' '.$lb_ds.' ';
                if ($c == 0) {$c = '';} else {$c = $c.' '.$lb_hs.' ';}
                if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms.' ';}
                if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}

                echo $xb.':'.$xc.':'.$xe.':'.$xq;
                echo '<br>'.$b.$c.$e.$q;
            }

        } else {

            // Less than a day
            // Calculate hours
            $c = $x / $t;

            // Get everything before the separator '.'
            if (false !== ($cal = strpos($c, '.'))) {
                $c = substr($c, 0, $cal);
            }

            // $z = what's left
            $z = $x - ($t * $c);

            // Calculate minutes
            $e = $z / $m;

            // Get everything before the separator '.'
            if (false !== ($cal = strpos($e, '.'))) {
                $e = substr($e, 0, $cal);
            }

            // $q = what's left
            $q = $z - ($m * $e);

            // Rewrite numbers if below 9
            if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
            if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
            if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }

            // Rewrite labels
            if ($c <= 1) { $lb_hs = $lb_h; }
            if ($e <= 1) { $lb_ms = $lb_m; }
            if ($q <= 1) { $lb_ss = $lb_s; }

            // if == 0 - do not show
            $c = $c.' '.$lb_hs.' ';
            if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms.' ';}
            if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}

            echo $xc.':'.$xe.':'.$xq;
            echo '<br>'.$c.$e.$q;

        }

    } else {

        // Less than an hour
        // Calculate minutes
        $e = $x / $m;

        // Get everything before the separator '.'
        if (false !== ($cal = strpos($e, '.'))) {
            $e = substr($e, 0, $cal);
        }

        // $q = what's left
        $q = $x - ($m * $e);

        // Rewrite numbers if below 9
        if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
        if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }

        // Rewrite labels
        if ($e <= 1) { $lb_ms = $lb_m; }
        if ($q <= 1) { $lb_ss = $lb_s; }

        // if == 0 - do not show
        $e = $e.' '.$lb_ms.' ';
        if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}

        echo $xe.':'.$xq;
        echo '<br>'.$e.$q;

    }

} else {

    // Less than a minute
    // Only secounds

    // Rewrite numbers if below 9
    if ($x <= 9) { $xx = '0'.$x; } else { $xx = $x; }

    // Rewrite labels
    if ($x <= 1) { $lb_ss = $lb_s; }

    // if == 0 - do not show
    $x = $x.' '.$lb_ss;

    echo '00:'.$xx;
    echo '<br>'.$x;

}
?>

<form action="" method="post"><input name="number" type="text"><input name="" type="submit" value="Submit"></form>

Thanks everybody for your help.. here is the simplified and done script.. I have a new problem though.. when you type less than 1576 in the field, it writes some different numbers than I want it to…

EDIT: Problem now fixed.. Welcome to use the script 😀

<?
if (isset($_POST['number'])) {
    $x = $_POST['number'];
} else {
    $x = 54098;
}

// Labels
$lb_y = 'year';
$lb_ys = 'years';
$lb_d = 'day';
$lb_ds = 'days';
$lb_h = 'hour';
$lb_hs = 'hours';
$lb_m = 'minute';
$lb_ms = 'minutes';
$lb_s = 'second';
$lb_ss = 'seconds';
$lb_and = 'and';

//$x = 54098; // Time in seconds - change to $row['time'];

$f = 15768000; // seconds in a year
$d = 43200; // seconds in a day
$h = 3600; // seconds in an hour
$m = 60; // seconds in a minute

$a = $x / $f;
if (false !== ($cal = strpos($a, 'E-'))) { $a = 0; }
if (false !== ($cal = strpos($a, '.'))) { $a = substr($a, 0, $cal); }
if ($a <= 0) { $a = 0; }
$b = ($x - ($f * $a)) / $d;
if (false !== ($cal = strpos($b, 'E-'))) { $b = 0; }
if (false !== ($cal = strpos($b, '.'))) { $b = substr($b, 0, $cal); }
if ($b <= 0) { $b = 0; }
$c = ($x - ($f * $a) - ($d * $b)) / $h;
if (false !== ($cal = strpos($c, 'E-'))) { $c = 0; }
if (false !== ($cal = strpos($c, '.'))) { $c = substr($c, 0, $cal); }
if ($c <= 0) { $c = 0; }
$e = ($x - ($f * $a) - ($d * $b) - ($h * $c)) / $m;
if (false !== ($cal = strpos($e, '.'))) { $e = substr($e, 0, $cal); }
if ($e <= 0) { $e = 0; }
$q = ($x - ($f * $a) - ($d * $b) - ($h * $c) - ($m * $e));

// Rewrite numbers if below 9
if ($a <= 9) { $xa = '0'.$a; } else { $xa = $a; }
if ($b <= 9) { $xb = '0'.$b; } else { $xb = $b; }
if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }

// Rewrite labels
if ($a <= 1) { $lb_ys = $lb_y; }
if ($b <= 1) { $lb_ds = $lb_d; }
if ($c <= 1) { $lb_hs = $lb_h; }
if ($e <= 1) { $lb_ms = $lb_m; }
if ($q <= 1) { $lb_ss = $lb_s; }

// if == 0 - do not show
if ($a == 0) {$a = '';} else {$a = $a.' '.$lb_ys;}
if ($b == 0) {$b = '';} else {$b = $b.' '.$lb_ds;}
if ($c == 0) {$c = '';} else {$c = $c.' '.$lb_hs;}
if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms;}
if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}

echo $xa.':'.$xb.':'.$xc.':'.$xe.':'.$xq.'<br>';

$time = array($a, $b, $c, $e, $q);

$time = array_filter($time);
$count = count($time);
$last = array_pop($time);
if ($count == 1) {
    $string = $last;
} elseif ($count == 0) {
    $string = '<i>No Time described</i>';
} else {
    $string = implode(', ', $time) . ' '.$lb_and.' ' . $last;
}

echo '<br>'.$string;

?>
<br><br>
<form action="" method="post"><input name="number" type="text"><input name="" type="submit" value="Submit"></form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:06:23+00:00Added an answer on June 17, 2026 at 4:06 pm

    A quick fix may be:

    $string = '15 hours 6 minutes 2 seconds';
    $pattern ='/ \d+ \w+$/';
    $string = preg_replace($pattern, ' and$0', $string);
    

    However, you may want to look into a nicer solution which resulted in the following array being built:

    $time = array($a, $b, $c, $e, $q);
    

    You could then do:

    $time = array_filter($time);
    $last = array_pop($time);
    $string = implode(', ', $time) . ' and ' . $last;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have build C# program that work with RAPI (communication to PPC or WinCE)
I have a cube that I have build that has data across multiple servers.
I have a code that I have successfully installed on several calculating clusters that
i have a view controller that uses the getDistance method for calculating the distance
I'm doing a homework assignment in C. I have to build a calculator that
I have a query that uses a lot of aggregate functions to build data
i have to build some financial data report, and for making the calculation, there
i have build a small function whose work to get value in alert box
I have build a webapplication using ASP.NET MVC and JQuery. On my local machine
I have build a c# class library verification.dll using OpenCVSharp. This references OpenCvSharp.dll in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.