Add 'x' number of hours to date
You may use something like the strtotime()
function to add something to the current timestamp. $new_time = date("Y-m-d H:i:s", strtotime('+5 hours'))
.
If you need variables in the function, you must use double quotes then like strtotime("+{$hours} hours")
, however better you use strtotime(sprintf("+%d hours", $hours))
then.
How to add hours to a Date object?
JavaScript itself has terrible Date/Time API's. Nonetheless, you can do this in pure JavaScript:
Date.prototype.addHours = function(h) {
this.setTime(this.getTime() + (h*60*60*1000));
return this;
}
PHP Add one hour to current date time
$my_date_time = date("Y-m-d H:i:s", strtotime("+1 hours"))
Construct a new date by adding X number of hours (Unix Shell)
You can use:
# your date variable
dt=$(date -d '2016-08-15 11:10:15')
# add 1 hour to $dt now
date -d "$dt +1 hour"
Mon Aug 15 12:10:15 EDT 2016
PHP Add 4 hours to date
Does this work?
$date = $dates[0] < $dates[1] ? $dates[1] : $dates[0];
$new_date = date("Y-m-d H:i:s", strtotime('+4 hours', strtotime($date)));
echo $date;
echo "<br/>". $new_date;
You were passing $date
AS the second argument of strtotime, and it's not a UNIX timestamp (as you said). So with strtotime again, you should convert it in a UNIX timestamp, and then pass it to strtotime.
Javascript add hours to time
for adding hours, use setHours
:
// Gets the current timevar now = new Date();
console.log("actual time:", now);
now.setHours(now.getHours() + 1)
console.log("actual time + 1 hour:", now);
Related Topics
PHP String Concatenation, Performance
PHP List All Files in Directory
Why Are My PHP Files Showing as Plain Text
Connect to SQL Server Through Pdo Using SQL Server Driver
Allowed Memory Size of X Bytes Exhausted
MySQLi Fetch_All() Not a Valid Function
Warning: MySQL_Fetch_Array() Expects Parameter 1 to Be Resource, Boolean Given In
Difference Between "Not Equal" Operators ≪≫ and != in PHP
PHP: How to Check If Image File Exists
In a PHP/Apache/Linux Context, Why Exactly Is Chmod 777 Dangerous
Get Response from PHP File Using Ajax
How to Install a Specific Version of Package Using Composer
PHP Equivalent of .Net/Java'S Tostring()
What Is Function Overloading and Overriding in PHP