How to Add 24 Hours to a Unix Timestamp in PHP

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

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);

Adding 1 day / 24 hours to different unixtimestamp in the same query

EDIT:

I took a minute to read up on unix timestamp. and found out it is basically numbers of seconds. So what you probably will have to do is add 24 hours worth of seconds to the existing number in your database:

UPDATE table SET expire_time = expire_time+86400

This will add 86400 to the existing value, which is the same as 24 hours.

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 two time more than 24 hours

You have a logical error in your code, you just simply sum every part of the time but the thing you need to do is this :

If number of seconds is superior to 60 you have to get only the result of $number_seconds%60 and add it to your minutes and repeat it with the minutes, let me show you :

       function TimeSum($a,$b)
{
list ($hour1, $min1, $sec1) = explode(':', $a);
list ($hour2, $min2, $sec2) = explode(':', $b);
//counting number of seconds and getting extra minutes outs
$total_sec = $sec1+$sec2;
$sumSec = $total_sec%60;
$extra_min = ($total_sec-$sumSec)/60;
//counting number of minutes and getting extra hours outs
$total_min= $min1+$min2+$extra_min;
$sumMin = $total_min%60;
$extra_hr = ($total_min-$sumMin)/60;
//counting number of hours
$sumHour = $hour1 + $hour2 + $extra_hr;

return $sumHour.':'.$sumMin.':'.$sumSec;
}

Adding two hours to php-timestamp-script

Just add 2 hours to the unix timestamp you get from filemtime:

$modtime=date("M j Y g:i A", (filemtime($dirArray[$index]) + (2*60 * 60));

You can't send it as a separate parameter, filemtime will return an integer so just add on to it.

Given a Unix timestamp, how to get beginning and end of that day?

strtotime can be used to to quickly chop off the hour/minutes/seconds

$beginOfDay = strtotime("today", $timestamp);
$endOfDay = strtotime("tomorrow", $beginOfDay) - 1;

DateTime can also be used, though requires a few extra steps to get from a long timestamp

$dtNow = new DateTime();
// Set a non-default timezone if needed
$dtNow->setTimezone(new DateTimeZone('Pacific/Chatham'));
$dtNow->setTimestamp($timestamp);

$beginOfDay = clone $dtNow;
$beginOfDay->modify('today');

$endOfDay = clone $beginOfDay;
$endOfDay->modify('tomorrow');
// adjust from the start of next day to the end of the day,
// per original question
// Decremented the second as a long timestamp rather than the
// DateTime object, due to oddities around modifying
// into skipped hours of day-lights-saving.
$endOfDateTimestamp = $endOfDay->getTimestamp();
$endOfDay->setTimestamp($endOfDateTimestamp - 1);

var_dump(
array(
'time ' => $dtNow->format('Y-m-d H:i:s e'),
'start' => $beginOfDay->format('Y-m-d H:i:s e'),
'end ' => $endOfDay->format('Y-m-d H:i:s e'),
)
);

With the addition of extended time in PHP7, there is potential to miss a second if using $now <= $end checking with this.
Using $now < $nextStart checking would avoid that gap, in addition to the oddities around subtracting seconds and daylight savings in PHP's time handling.

How to round unix timestamp up and down to nearest half hour?

Use modulo.

$prev = 1330518155 - (1330518155 % 1800);
$next = $prev + 1800;

The modulo operator gives the remainder part of division.



Related Topics



Leave a reply



Submit