PHP converts seconds to days, hours, and minutes to format time

Source: Internet
Author: User
Tags sprintf
PHP convert seconds to days, hours, and minutes format time simple notation

function Secstostr ($secs) {    if ($secs >=86400) {$days =floor ($secs/86400); $secs = $secs%86400; $r = $days. ' Day '; if ($days <>1) {$r. = ' s ';} if ($secs >0) {$r. = ', ';}}    if ($secs >=3600) {$hours =floor ($secs/3600), $secs = $secs%3600; $r. = $hours. ' Hour '; if ($hours <>1) {$r. = ' s ';} if ($secs >0) {$r. = ', ';}}    if ($secs >=60) {$minutes =floor ($secs/60), $secs = $secs%60; $r. = $minutes. ' Minute '; if ($minutes <>1) {$r. = ' s ';} if ($secs >0) {$r. = ', ';}}    $r. = $secs. ' Second '; if ($secs <>1) {$r. = ' s ';}    return $r;}

Two. PHP converts less than a few seconds into days, hours, and minutes in full format

/** * Time-friendly hint style (that is, the micro-blog in XXX hours ago, yesterday, etc.) * * that is, the micro-blog xxx hours ago, yesterday and so on, the time is more than $time _limit returned by the Out_format set style timestamp * * @param int * @p  Aram int * @param string * @param array * @param int * @return string */function date_friendly ($timestamp, $time _limit        = 604800, $out _format = ' y-m-d h:i ', $formats = null, $time _now = null) {if (get_setting (' time_style ') = = ' N ') {    Return Date ($out _format, $timestamp);    } if (! $timestamp) {return false; if ($formats = = null) {$formats = Array (' Year ' = Aws_app::lang ()->_t ('%s years ago '), ' MONTH ' = Aws_ap P::lang ()->_t ('%s months ago '), ' day ' = + Aws_app::lang ()->_t ('%s days ago '), ' HOUR ' = Aws_app::lang ()->_t ('%s hours ago '), '    MINUTE ' = Aws_app::lang ()->_t ('%s minutes ago '), ' SECOND ' = Aws_app::lang ()->_t ('%s seconds ago ')); } $time _now = $time _now = = null?    Time (): $time _now;    $seconds = $time _now-$timestamp;    if ($seconds = = 0) {$seconds = 1; } if (! $time _limit OR $seconds > $time _limiT) {return date ($out _format, $timestamp);    } $minutes = Floor ($seconds/60);    $hours = Floor ($minutes/60);    $days = Floor ($hours/24);    $months = Floor ($days/30);    $years = Floor ($months/12);    if ($years > 0) {$diffFormat = ' year ';        } else {if ($months > 0) {$diffFormat = ' MONTH ';            } else {if ($days > 0) {$diffFormat = ' Day ';                 } else {if ($hours > 0) {$diffFormat = ' HOUR '; } else {$diffFormat = ($minutes > 0)?                ' MINUTE ': ' SECOND ';    }}}} $dateDiff = null;            Switch ($diffFormat) {case ' year ': $dateDiff = sprintf ($formats [$diffFormat], $years);        Break Case ' MONTH ': $dateDiff = sprintf ($formats [$difFformat], $months);        Break            Case ' Day ': $dateDiff = sprintf ($formats [$diffFormat], $days);        Break            Case ' HOUR ': $dateDiff = sprintf ($formats [$diffFormat], $hours);        Break            Case ' MINUTE ': $dateDiff = sprintf ($formats [$diffFormat], $minutes);        Break            Case ' SECOND ': $dateDiff = sprintf ($formats [$diffFormat], $seconds);    Break } return $dateDiff;}

Related articles:

PHP converts seconds to days, hours, and minutes

Time conversion in JS-a sample code that converts milliseconds into a datetime

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.