• We just launched and are currently in beta. Join us as we build and grow the community.

How To Get The Difference Between Two Dates In PHP

Zoark

System Reliability Specialist
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
186
Likes
120
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Good Day!!!

In this article, we are going to learn on How To Get The Difference Between Two Dates In PHP. In the PHP, to calculate the difference between two dates has many ways to do this. To get the difference of hours between two dates, we have to use the functions of datetime.

We have the start date and end date to get their difference hour. strtotime() we are going to use to calculate the two dates difference.

HTML Date Input

This is the input field of date to get the start date and end date came from the user.

  1. <center

    >
  2. <form

    id

    =

    "twoDatesDifference"

    action

    =

    ""

    method

    =

    "post"

    >
  3. <div

    >
  4. <label

    style

    =

    "padding-top:20px; font-size:18px; font-weight:bold;"

    >

    Start Of Date</

    label

    >
  5. <br

    /

    >
  6. <input

    type

    =

    "date"

    name

    =

    "startOf_Date"

    value

    =

    "<?php if(!empty($_POST["

    startOf_Date"])) { echo $_POST["

    startOf_Date"]; } ?>

    " class="date_textBox">
  7. </

    div

    >
  8. <div

    >
  9. <label

    style

    =

    "padding-top:20px; font-size:18px; font-weight:bold;"

    >

    End Of Date</

    label

    >
  10. <br

    /

    >
  11. <input

    type

    =

    "date"

    name

    =

    "endOf_Date"

    value

    =

    "<?php if(!empty($_POST["

    startOf_Date"])) { echo $_POST["

    endOf_Date"]; } ?>

    " class="date_textBox">
  12. </

    div

    >
  13. <div

    >
  14. <input

    type

    =

    "submit"

    name

    =

    "enter"

    value

    =

    "Enter"

    class

    =

    "btn_difference"

    >
  15. </

    div

    >
  16. </

    form

    >
  17. </

    center

    >

PHP Function

This is the PHP function, and this is the codes that the strtotime() function is used to calculate the hours difference of two dates given by the user.

  1. <?php
  2. function

    differenceIn_TwoDates(

    $startOf_Date

    ,

    $endOf_Date

    )

    {
  3. $timestamp_start

    =

    strtotime

    (

    $startOf_Date

    )

    ;
  4. $timestamp_end

    =

    strtotime

    (

    $endOf_Date

    )

    ;
  5. $difference_Dates

    =

    abs

    (

    $timestamp_end

    -

    $timestamp_start

    )

    /

    3600

    ;
  6. return

    $difference_Dates

    ;
  7. }
  8. if

    (

    !

    empty

    (

    $_POST

    [

    "enter"

    ]

    )

    )

    {
  9. $dates_difference

    =

    differenceIn_TwoDates(

    $_POST

    [

    "startOf_Date"

    ]

    ,

    $_POST

    [

    "endOf_Date"

    ]

    )

    ;
  10. echo

    "<h3 style='color:red; text-align:center;'>The Difference of Hour in Two Dates is <b style='color:blue;'>"

    .

    $dates_difference

    .

    " hours.</b></h3>"

    ;
  11. }
  12. ?>

And, this is the style.

  1. <style type=

    "text/css"

    >
  2. #twoDatesDifference

    {
  3. border-top

    :

    red

    2px

    solid

    ;
  4. padding

    :

    10px

    ;
  5. width

    :

    500px

    ;
  6. }
  7. #twoDatesDifference

    div {
  8. margin-bottom

    :

    15px
  9. }
  10. #twoDatesDifference

    div label {
  11. margin-left

    :

    5px
  12. }
  13. .date_textBox

    {
  14. padding

    :

    10px

    ;
  15. border

    :

    #F0F0F0

    1px

    solid

    ;
  16. border-radius

    :

    4px

    ;
  17. background-color

    :

    #FFF

    ;
  18. cursor

    :

    pointer

    ;
  19. font-size

    :

    18px

    ;
  20. }
  21. .btn_difference

    {
  22. background-color

    :

    azure

    ;
  23. color

    :

    black

    ;
  24. font-weight

    :

    bold

    ;
  25. font-size

    :

    18px

    ;
  26. border

    :

    red

    1px

    solid

    ;
  27. border-radius

    :

    4px

    ;
  28. padding

    :

    10px

    ;
  29. width

    :

    200px

    ;
  30. cursor

    :

    pointer

    ;
  31. }
  32. .btn_difference

    :

    hover

    {
  33. background-color

    :

    black

    ;
  34. color

    :

    azure

    ;
  35. font-weight

    :

    bold

    ;
  36. font-size

    :

    18px

    ;
  37. border

    :

    red

    1px

    solid

    ;
  38. border-radius

    :

    4px

    ;
  39. padding

    :

    10px

    ;
  40. width

    :

    200px

    ;
  41. cursor

    :

    pointer

    ;
  42. }
  43. </style>

Share us your thoughts and comments below. Thank you so much for dropping by and reading this blog post. For more updates, don’t hesitate and feel free to visit our website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,491

323,080

323,089

Top