Home >Backend Development >PHP Tutorial >PHP calculates the difference between two timestamps

PHP calculates the difference between two timestamps

WBOY
WBOYOriginal
2016-07-29 09:06:371511browse

PHP calculates the difference between two timestamps

<code><span>/**
     * 计算两个时间戳之差
     *<span> @param</span> $begin_time
     *<span> @param</span> $end_time
     *<span> @return</span> array
     */</span><span><span>function</span><span>timeDiff</span><span>( <span>$begin_time</span>, <span>$end_time</span> )</span>{</span><span>if</span> ( <span>$begin_time</span> < <span>$end_time</span> ) {
            <span>$starttime</span> = <span>$begin_time</span>;
            <span>$endtime</span> = <span>$end_time</span>;
        } <span>else</span> {
            <span>$starttime</span> = <span>$end_time</span>;
            <span>$endtime</span> = <span>$begin_time</span>;
        }
        <span>$timediff</span> = <span>$endtime</span> - <span>$starttime</span>;
        <span>$days</span> = intval( <span>$timediff</span> / <span>86400</span> );
        <span>$remain</span> = <span>$timediff</span> % <span>86400</span>;
        <span>$hours</span> = intval( <span>$remain</span> / <span>3600</span> );
        <span>$remain</span> = <span>$remain</span> % <span>3600</span>;
        <span>$mins</span> = intval( <span>$remain</span> / <span>60</span> );
        <span>$secs</span> = <span>$remain</span> % <span>60</span>;
        <span>$res</span> = <span>array</span>( <span>"day"</span> => <span>$days</span>, <span>"hour"</span> => <span>$hours</span>, <span>"min"</span> => <span>$mins</span>, <span>"sec"</span> => <span>$secs</span> );
        Log::info(<span>$res</span>);
        <span>return</span><span>$res</span>;
    }</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces how PHP calculates the difference between two timestamps, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn