Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add 24 hours to a unix timestamp in php?

Tags:

php

timestamp

People also ask

How do you add hours to a timestamp?

2. In above formulas, 1 indicates to add one hour or one minute or one second, you can change it as you need. 3. If you want to add hours, minutes and second to a date simultaneously, you can use this formula =A2+TIME(23,23,34), this means to add 23 hours, 23 minutes and 34 seconds to a date cell.

How long is a day in Unix timestamp?

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

What is Unix timestamp in PHP?

What is the Unix timestamp? This is the number of seconds since the Unix epoch time (01-01-1970) . For more tips on the PHP date() function, we can see this post.


You probably want to add one day rather than 24 hours. Not all days have 24 hours due to (among other circumstances) daylight saving time:

strtotime('+1 day', $timestamp);

A Unix timestamp is simply the number of seconds since January the first 1970, so to add 24 hours to a Unix timestamp we just add the number of seconds in 24 hours. (24 * 60 *60)

time() + 24*60*60;

Add 24*3600 which is the number of seconds in 24Hours


Unix timestamp is in seconds, so simply add the corresponding number of seconds to the timestamp:

$timeInFuture = time() + (60 * 60 * 24);

You could use the DateTime class as well:

$timestamp = mktime(15, 30, 00, 3, 28, 2015);

$d = new DateTime();
$d->setTimestamp($timestamp);

Add a Period of 1 Day:

$d->add(new DateInterval('P1D'));
echo $d->format('c');

See DateInterval for more details.


As you have said if you want to add 24 hours to the timestamp for right now then simply you can do:

 <?php echo strtotime('+1 day'); ?>

Above code will add 1 day or 24 hours to your current timestamp.

in place of +1 day you can take whatever you want, As php manual says strtotime can Parse about any English textual datetime description into a Unix timestamp.

examples from the manual are as below:

<?php
     echo strtotime("now"), "\n";
     echo strtotime("10 September 2000"), "\n";
     echo strtotime("+1 day"), "\n";
     echo strtotime("+1 week"), "\n";
     echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
     echo strtotime("next Thursday"), "\n";
     echo strtotime("last Monday"), "\n";
?>

$time = date("H:i", strtotime($today . " +5 hours +30 minutes"));
//+5 hours +30 minutes     Time Zone +5:30 (Asia/Kolkata)