Get the App
SLTechnology News&Howtos  ›  Development  › 

How to calculate the time difference between two given dates by php

Shulou Source: shulou.com Published: 2022-06-03 17:09:46 09月13日 Update

This article mainly explains "how to find the time difference between two given dates by php". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to find the time difference between two given dates by php".

1. Convert two dates into timestamps first.

$startdate = strtotime ("{$year}-01-01"); $enddate = strtotime ("{$year}-{$month}-{$day}")

2. The two timestamps are subtracted.

(end time-start time)

$diff_seconds = $enddate-$startdate

In this way, you will get the time difference between two dates, but it is still counted in seconds, which is not good for reading.

Because there are 24 hours in a day, 60 minutes in an hour and 60 seconds in a minute; if we convert to 24 hours, 60 minutes and 86 400 seconds in a day, there are 86400 seconds in a day.

3. Divide the time difference $diff_seconds by 86400, and use floor () to round down to the nearest integer.

$time = floor (($diff_seconds) / 86400); $time = floor (($diff_seconds) / 86400)

4. What you get is the difference in days, excluding the day of x, and add another 1.

This is the total number of days up to a certain month of a certain year.

At this point, I believe you have a deeper understanding of "how to find the time difference between two given dates in php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Time two date time difference content days hours learning practical deeper interest unit practical practical simple operation integer method more friends website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL vpn Shulou Technology NVidia Xiaomi