How to add Days, Hours, Minutes, and Seconds to Datetime in PHP




How to add Days, Hours, Minutes, and Seconds to Datetime in PHP

Here we’ll provide the simplest way to add days, minutes, hours and seconds to time using PHP. In PHP, using date() and strtotime() function you can easily increase or decrease time. The provided PHP code lets you do the following works.

  • Add days to datetime.
  • Add hours to datetime.
  • Add minutes to datetime.
  • Add seconds to datetime.
$startTime = date("Y-m-d H:i:s");

//display the starting time
echo 'Starting Time: '.$startTime;

//add 1 hour to time
$cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 hour',strtotime($startTime)));

//display the converted time
echo 'Converted Time (added 1 hour): '.$cenvertedTime;

//add 1 hour and 30 minutes to time
$cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 hour +30 minutes',strtotime($startTime)));

//display the converted time
echo 'Converted Time (added 1 hour & 30 minutes): '.$cenvertedTime;

//add 1 hour, 30 minutes and 45 seconds to time
$cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 hour +30 minutes +45 seconds',strtotime($startTime)));

//display the converted time
echo 'Converted Time (added 1 hour, 30 minutes  & 45 seconds): '.$cenvertedTime;

//add 1 day, 1 hour, 30 minutes and 45 seconds to time
$cenvertedTime = date('Y-m-d H:i:s',strtotime('+1 day +1 hour +30 minutes +45 seconds',strtotime($startTime)));

//display the converted time
echo 'Converted Time (added 1 day, 1 hour, 30 minutes  & 45 seconds): '.$cenvertedTime;

Using the above code you can add time to current time or any desire time. To sub-track use the same code except -instead of +.



Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforJobs is an advanced hiring platform based on artificial intelligence, enabling recruiters to hire top talent effortlessly.

CrowdforJobs

CrowdforApps brings to you the well researched list of the most successful and finest App development companies, Web software developers.

CrowdforApps

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink
CFT

News & Blogs

7f219b78165ca3de8e1b5d68c5ed0834.jpg

Create a Simple CRUD Database App - Connecting ...

What is CRUD In computer programming, create, read, update, and delete are the four basic functi...

464bc819f41b6878fc27398415288c37.jpg

How to Use Web Scraping

Have you ever thought about web scrapping? What's the main benefit of it? How do you start us...

f398eadf2a894b69689a56edbb04bae8.png

How to Disable Submit Button After Form Submiss...

I will help you for how to disable submit button after click in jquery php. we can prevent double...

Top Authors

With good communication and writing skiils, Astha Sharma is a full-time content writer working wi...

Astha Sharma

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working i...

Lokesh Gupta

Amit Agrawal Founder and COO at Cyber Infrastructure (P) Limited which is an custom software deve...

Amit Agrawal

SB Components Ltd is a specialist UK manufacturer of protective cases for single board compu...

SB Components
CFT

Our Client Says