How to calculate day and night, number of days between two dates in PHP?

Spread the love with your friends

number of days between two dates in PHP?
number of days between two dates in PHP?

Dear friends, today we will discuss about the small but important things in PHP from date and time concept. So we are going to start all the things one by one.

So let’s start the important things which miss a developer or freshers. These important things are normally used in almost all projects those are based on PHP.

  1. How to calculate day and night in PHP:

Solution:

// calculate days & Night
$diff=strtotime($checkin)-strtotime($checkout);
$nights=abs(round($diff / 86400));
$days=abs($nights+1);

the above code will be used to calculate the difference between two times or can get day and night.

In the above example, we just need to put only two values. I have used only two variables named “$checkin” and the “$checkout”. Just put these two values and got the result in difference.

  1. Get Expiry date in PHP:

Solutions:

// get expiry date
$exp_date=date(“Y-m-d”, strtotime(“+”.$validity.” months”));

This above the method used to find out the expiry date in PHP in form of the months.

We just need to put only the one value in form of month in the variable named: “$validity”.

Suppose if we need the expiry date after 10 month then we need to put only value 10 instead “$validity” variable.

  1. How to get number of days between two dates in PHP?

Solution:

$no_of_days=round((strtotime($depart_time1[0])-strtotime(date(‘Y-m-d’)))/86400);

The above method of PHP will be use to find out the difference or find out the number of days between two days in PHP.

For this method we just need to put only one value in the given variables named: “depart_time1”. in the second parameters we just put the current date method. You can put any value respective your projects.

So guys these small things are most important. We made this post when someone ask us due to comment so, we collect all the details and share in just a single post.

Dear friends, If you have some issue during implementation and about the code then please mention below in the comment box.

If you have any suggest then you can also mention in the below comment box. Thanks.


Spread the love with your friends

Leave a Comment

PHP Code Snippets Powered By : XYZScripts.com