Home  >  Article  >  Backend Development  >  How to calculate how many days are left in php

How to calculate how many days are left in php

青灯夜游
青灯夜游Original
2022-04-26 11:34:513617browse

Calculation method: 1. Use the "time()" statement to obtain the current timestamp; 2. Use the "strtotime("date")" statement to convert the specified date into a timestamp; 3. Convert the two timestamps Subtract to get the time difference; 4. Use the "floor(time difference/86400)" statement to convert the time difference into a days difference to get the remaining days.

How to calculate how many days are left in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php calculation remains How many days

Implementation method:

  • Use the time() function to get the current timestamp

  • Use the strtotime() function to convert the specified time into a timestamp

  • Subtract the two timestamps to get the time difference

  • Divide the time difference by The total number of seconds in a day (24*60*60=86400), use floor() to round, and convert the time difference into a difference in days

Implementation example: Today is April 26 Number, let’s see how many days are left until May Day

<?php
header("Content-type:text/html;charset=utf-8");
//2022年5月1日 
$time1=strtotime("2022-5-1");
//当前时间戳
$time2=time();

$diff_seconds = $time1 - $time2;
$diff_days = floor($diff_seconds/86400);
echo "距离五一还剩: ".$diff_days." 天";
?>

How to calculate how many days are left in php

How to calculate how many days are left in php

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to calculate how many days are left in php. For more information, please follow other related articles on the PHP Chinese website!

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