Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1984

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T01:14:07+00:00 2024-11-26T01:14:07+00:00

How to find the no of days between two dates in PHP

  • 61k

There are times you would want to work with dates in your application and finding the difference between two dates is quite common. Handling dates in any programming language is always a tedious process so let me make a part of that easy for you in this post.

We are going to use 2 built in functions that PHP provides

  1. strtotime() – strtotime() converts a date into a Unix timestamp. The type of date formats that are allowed can be found in PHP's datetime documentation

  2. round() – round() is a mathematical function that rounds up a floating point number to its nearest integer e.g round(4.5) = 5, round(3.4) = 3 etc

Let's see how we would use this 2 functions to get what we need

<?php     $start_date = "3rd August 2022";     $end_date = "15th August 2022";      // ts means timestamp     $start_ts = strtotime($start_date);     $end_ts = strtotime($end_date);      $diff = $end_ts - $start_ts;      echo round($diff / 86400); // returns 12, that is 12 days 
Enter fullscreen mode Exit fullscreen mode

So what exactly is happening?

  • We create two variables to store the start & end dates we want to find the difference of.
  • The strtotime() method converts the dates into a unix timestamp format. We need to convert these two dates so that we can get the no of seconds from 1st January 1970 to the two dates. This is a very important step.
  • We substract the two dates from each other to get the difference in seconds.
  • We then divide the difference by 86400. Why 86400?, because that is the number of seconds in a day (60 seconds x 60 minutes x 24 hours). Dividing the difference by the total number of seconds in a day gives us the no of days we are looking for.
  • The last step is to round up the final answer into an integer.

You could even take this further and put it inside a function, so that it can be reusable in multiple places

<?php     function dateDiff($start, $end) {         $start_ts = strtotime($start);         $end_ts = strtotime($end);         $diff = $end_ts - $start_ts;          return round($diff / 86400);     } 
Enter fullscreen mode Exit fullscreen mode

This is quite easy and simple to build and has a lot of use cases where it is helpful. Do you have a better way of handling this issue?, i would like to know in the comments.

phpwebdev
  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.