"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Add Hours to a Date in PHP?

How to Add Hours to a Date in PHP?

Published on 2024-11-10
Browse:146

How to Add Hours to a Date in PHP?

Add Hours to Date with PHP

To manipulate dates and times in PHP, you can utilize the date(), strtotime(), and sprintf() functions. Here's how you can add a number of hours to the current date/time:

This code demonstrates how to add the specified number of hours to the current date/time and store it in the $new_time variable. The strtotime() function calculates the timestamp (seconds since the Unix epoch) based on the provided string representing a time period (in this case, " " followed by the number of hours and "hours").

By using sprintf() to dynamically format the number of hours, you gain the flexibility to pass in different values at runtime. Note that if you need to include variables within the strtotime() string, you must use double quotes (") and variable placeholders, as shown in the code. Also, the date() function formats the resulting timestamp into a human-readable date and time string.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3