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
echo date($result2["datetime"], strtotime("-48 hours"));
1 Day: 86,400 seconds. 1 Week: 604,800 seconds. 1 Month: 2,629,743 seconds (30.44 days on average)
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With