Logo
  • Home
  • About
  • Services
  • Blogs
  • Career
  • Contact
Logo

The UK's #1 Software Development Company. PodTech IO builds custom software solutions for businesses. Call today for a free consultation

  • Address

    4th Floor, 4 Tabernacle Street London EC2A 4LU
  • Email

    info@podtech.com
  • Contact

    +44 (0) 20 8720 6583

Readable Time – PHP, given a unix timestamp, return a readable time

  • Home
  • Readable Time – PHP, given a unix timestamp, return a readable time
  • By podtech
  • In PHP

Readable Time – PHP, given a UNIX timestamp, return a readable time


# Given a unix timestamp, returns in readable time ie "1 day, 7 hours, 23 minutes, 2 seconds"
function readable_time($timestamp, $num_times = 2)
{
        //this returns human readable time when it was uploaded (array in seconds)
        $times = array(31536000 => 'year', 2592000 => 'month',  604800 => 'week', 
86400 => 'day', 3600 => 'hour', 60 => 'min', 1 => 'sec');
        $now = time();

        $secs = $timestamp;
        //Fix so that something is always displayed
        if ($secs == 0)
        {
               $secs = 1;
        }

        $count = 0;
        $time = '';

        foreach ($times AS $key => $value)
        {
                if ($secs >= $key)
                {
                        //time found
                        $s = '';
                        $time .= floor($secs / $key);

                        if ((floor($secs / $key) != 1))
                        {
                                $s = 's';
                        }

                        $time .= ' ' . $value . $s;
                        $count++;
                        $secs = $secs % $key;

                        if ($count > $num_times - 1 || $secs == 0)
                        {
                                break;
                        }
                        else
                        {
                                $time .= ', ';
                        }
                }
        }
        return $time;
}
given a unix timestampPHPReadable Timereturn a readable time
Add a timestamp to every STDOUT line
Given a size in KB, try to return a more readable MB, GB or TB answer string

Recent Posts

  • DevOps as a Software Development Methodology
  • How Does Spiral Software Development Methodology Work?
  • Waterfall Software Development Methodology And When To Use It
  • What Exactly Is Lean Software Development Methodology?
  • What is Kanban and How Does It Work?

PodTech IO

The UK's #1 Software Development Company. PodTech IO builds custom software solutions for businesses. Call today for a free consultation

Company

  • About
  • Services
  • Contact
  • Blogs
  • Career

Terms

  • Terms & Conditions
  • Privacy Policy

Contact Us

  • ADDRESS

    Headquarter: Tabernacle Street London India: Vi john Tower, Gurgaon, Haryana
  • EMAIL

    info@podtech.com
  • CONTACT

    +44 (0) 20 8720 6583

Copyright ©2023 PodTech IO. All Rights Reserved

Logo