How to add 1 or more hours and days in PHP current date and time ?

Spread the love with your friends

How to add hours and days in current time in php
How to add hours and days in current time in php

Hello friends, How are you all. Friends today we will learn about date in PHP, that how to add or subtract hours in current date and time. Most of developers faces issue in PHP that how to add 1 or custom hours in a current date and we will also know how to add a day in a current date or how to less few day/days from a current date and time. So let’s start this interesting topic.

So our topic will be :

  1. How to add or less hours from a current time in PHP ?

  2. How to add or less days from a current date in PHP ?

First of all we will learn the first above point.

So lets start with a demonstration of code :

  1. How to add or less hours from a current time in PHP ?

<!DOCTYPE html>
<html>
<body>
<?php
$new_time = date(“h:i:s A”, strtotime(‘-1 hours’));
echo $new_time;
?>
</body>
</html>

In this code , we got time before 1 hour from a current time.  if you want to get previous time in any hours later then you need to increase number of hours, suppose i want to get time 2 hours before from the current time then we need to implement this code like this :

$new_time = date(“h:i:s A”, strtotime(‘-2 hours’));

You can get the time after a current time, suppose i want the time 1 hour later from now then the implementation of code will be like this :

$new_time = date(“h:i:s A”, strtotime(‘+1 hours’));

Or we you want to change the formation of time then you can also change the format. Suppose i want the hour only from the time hours : minutes : seconds

then the implementation of code will be like this :

$new_time = date(“h A”, strtotime(‘-1 hours’));

So i think guys you learn very well the less and after custom hours in php with the current time. So now we will move on next point that given above.

  1. How to add or less days from a current date in PHP ?

in this topic , we will learn how to get before and days date from a given date in PHP.

So let’s start the code :

<!DOCTYPE html>
<html>
<body>
<?php
$new_date = date(“Y-m-d”, strtotime(“2022-03-11 +10 days”));
echo $new_date;
?>
</body>
</html>

In this code of PHP,  i want to get 10 days later date from a current date, so i add 10 days later time in a current date.

if you want to get the date before a 10 days or any custom days then the code will be like this:

<?php

$new_date = date(“Y-m-d”, strtotime(“2022-03-11  -10 days”));

echo $new_date;

?>

After this code we will got the date 10 days before from the current date. Suppose today date is “2022-03-11” then the result will be “2022-03-01”.

So i think you understood this example very well. So guys if you have any suggest about the post and point about this topic then please mention in comment box below. Thanks.


Spread the love with your friends

Leave a Comment

PHP Code Snippets Powered By : XYZScripts.com