Home > Backend Development > PHP Tutorial > 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

藏色散人
Release: 2023-04-09 21:52:01
forward
4762 people have browsed it

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();
Copy after login

The fastest method

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

fastermethod

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

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;
}
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template