Zoark
System Reliability Specialist
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.
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.
And, this is the 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
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.
- <center
>
- <form
id
=
"twoDatesDifference"
action
=
""
method
=
"post"
>
- <div
>
- <label
style
=
"padding-top:20px; font-size:18px; font-weight:bold;"
>
Start Of Date</
label
>
- <br
/
>
- <input
type
=
"date"
name
=
"startOf_Date"
value
=
"<?php if(!empty($_POST["
startOf_Date"])) { echo $_POST["
startOf_Date"]; } ?>
" class="date_textBox">
- </
div
>
- <div
>
- <label
style
=
"padding-top:20px; font-size:18px; font-weight:bold;"
>
End Of Date</
label
>
- <br
/
>
- <input
type
=
"date"
name
=
"endOf_Date"
value
=
"<?php if(!empty($_POST["
startOf_Date"])) { echo $_POST["
endOf_Date"]; } ?>
" class="date_textBox">
- </
div
>
- <div
>
- <input
type
=
"submit"
name
=
"enter"
value
=
"Enter"
class
=
"btn_difference"
>
- </
div
>
- </
form
>
- </
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.
- <?php
- function
differenceIn_TwoDates(
$startOf_Date
,
$endOf_Date
)
{
- $timestamp_start
=
strtotime
(
$startOf_Date
)
;
- $timestamp_end
=
strtotime
(
$endOf_Date
)
;
- $difference_Dates
=
abs
(
$timestamp_end
-
$timestamp_start
)
/
3600
;
- return
$difference_Dates
;
- }
- if
(
!
empty
(
$_POST
[
"enter"
]
)
)
{
- $dates_difference
=
differenceIn_TwoDates(
$_POST
[
"startOf_Date"
]
,
$_POST
[
"endOf_Date"
]
)
;
- 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>"
;
- }
- ?>
And, this is the style.
- <style type=
"text/css"
>
- #twoDatesDifference
{
- border-top
:
red
2px
solid
;
- padding
:
10px
;
- width
:
500px
;
- }
- #twoDatesDifference
div {
- margin-bottom
:
15px
- }
- #twoDatesDifference
div label {
- margin-left
:
5px
- }
- .date_textBox
{
- padding
:
10px
;
- border
:
#F0F0F0
1px
solid
;
- border-radius
:
4px
;
- background-color
:
#FFF
;
- cursor
:
pointer
;
- font-size
:
18px
;
- }
- .btn_difference
{
- background-color
:
azure
;
- color
:
black
;
- font-weight
:
bold
;
- font-size
:
18px
;
- border
:
red
1px
solid
;
- border-radius
:
4px
;
- padding
:
10px
;
- width
:
200px
;
- cursor
:
pointer
;
- }
- .btn_difference
:
hover
{
- background-color
:
black
;
- color
:
azure
;
- font-weight
:
bold
;
- font-size
:
18px
;
- border
:
red
1px
solid
;
- border-radius
:
4px
;
- padding
:
10px
;
- width
:
200px
;
- cursor
:
pointer
;
- }
- </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.