Home  >  Article  >  Backend Development  >  Share the most convenient and fastest way to calculate the remaining time of the day in PHP

Share the most convenient and fastest way to calculate the remaining time of the day in PHP

藏色散人
藏色散人forward
2021-04-22 11:15:294707browse

This article will introduce you to the most convenient and fastest way to calculate the remaining time of the day in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

[Recommended learning: PHP video tutorial]

The most convenient method

echo strtotime('23:59:59')-time();

The fastest method

$now = time();
echo 86400-date('H', $now)*3600- date('i', $now)*60-date('s');

fastermethod

echo 86400-(time()+8*3600)%86400;

Attached test data

xubanditdeMacBook-Pro:~ xubandit$ time php -f a.php

real    0m31.146s
user    0m30.966s
sys    0m0.075s
xubanditdeMacBook-Pro:~ xubandit$ time php -f b.php

real    0m48.574s
user    0m48.329s
sys    0m0.098s
xubanditdeMacBook-Pro:~ xubandit$ time php -f c.php

real    0m11.156s
user    0m10.786s
sys    0m0.098s
xubanditdeMacBook-Pro:~ xubandit$ cat a.php
<?php
for($i=0;$i<10000000;$i++){
    strtotime('23:59:59')-time();
}
xubanditdeMacBook-Pro:~ xubandit$ cat b.php
<?php
for($i=0;$i<10000000;$i++){
    $now = time();
    86400-date('H', $now)*3600- date('i', $now)*60-date('s');
}
xubanditdeMacBook-Pro:~ xubandit$ cat c.php
<?php
for($i=0;$i<10000000;$i++){
    86400-(time()+8*3600)%86400;
}

The above is the detailed content of Share the most convenient and fastest way to calculate the remaining time of the day in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete