Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add 48 hours to a timestamp

I am using this code to minus 48 hours from a timestamp

echo date($result2["datetime"], strtotime("-48 hours"));

This works fine, i want to add 48 hours, i have tried:

echo date($result2["datetime"], strtotime("+48 hours"));

I have echoed $result2["datetime"]; which shows a timestamp in the format Y-m-d H:i:s

and when i use:

echo date('Y-m-d H:i:s', strtotime("+48 hours"));

that adds the 48 hours on fine too

When i use

echo date($result2["datetime"], strtotime("+48 hours"));

Its just echoing the same timestamp thats returned from $result2["datetime"]; and not +48 hours

like image 650
user3843997 Avatar asked Oct 21 '14 19:10

user3843997


People also ask

How do I add 48 hours to a timestamp?

echo date($result2["datetime"], strtotime("-48 hours"));

How much is a day in timestamp?

1 Day: 86,400 seconds. 1 Week: 604,800 seconds. 1 Month: 2,629,743 seconds (30.44 days on average)

How do I add a 24 hour timestamp to Unix?

The Unix timestamp is designed to track time as a running total of seconds from the Unix Epoch on January 1st, 1970 at UTC. To add 24 hours to a Unix timestamp we can use any of these methods: Method 1: Convert 24 hours to seconds and add the result to current Unix time. echo time() + (24*60*60);


1 Answers

The first parameter for date() is the format you want the date output in. The second parameter is the value you wish to format. There you will use strtotime() to add your 48 hours.

echo date('Y-m-d H:i:s', strtotime($result2["datetime"] . " +48 hours"));

Demo

or:

echo date('Y-m-d H:i:s', strtotime("+48 hours", strtotime($result2["datetime"])));

Demo

This is kind of ugly, though. I recommend using DateTime() instead:

echo (new DateTime($result2["datetime"]))->modify('+48 hours')->format('Y-m-d H:i:s');

Demo

like image 198
John Conde Avatar answered Oct 17 '22 12:10

John Conde