How to convert date to timestamp, add 6 hours and convert it back to date in PHP?

how to add 6 hours to this line?

$ parent = "2011-08-04 15:00:01";

I think the best way is to convert it to timestamp, add 21600 seconds, and then convert it back to date.

How to do it?

Thanks in advance.

+1
source share
6 answers

Perhaps you are looking for the function http://www.php.net/manual/en/function.strtotime.php

$timestamp = strtotime("2011-08-04 15:00:01");
$timestamp += 6 * 3600;
echo date('Y-m-d H:i:s', $timestamp);
+5
source
$sixhours_from_parent = strtotime($parent) + 21600;
$sixhours_date = date('Y-m-d H:i:s', $sixhours_from_parent);
+1
source
<?php
$parent = "2011-08-04 15:00:01";
$parentTime = strtotime($parent);
$later = strtotime("+6 hours", $parentTime);
echo date('Y-m-d H:i:s', $later);
?>
+1

timestamp/datetime SQL.

SELECT datefield + INTERVAL 6 HOUR
0
date('Y-m-d H:i:s', strtotime($parent) + 21600);
0

For PHP> 5.3, you can use the DateInterval class for the DateTime object, which seems to me to be the easiest way to handle the complexity of time calculations. So in your case, you can do something like this:

$time = new \DateTime("2011-08-04 15:00:01");
$time->add(new \DateInterval('PT6H')); //add six hours
echo $time->format('Y-m-d H:i:s');

Link

0
source

All Articles