Open In App

How to Add 24 Hours to a Unix Timestamp in PHP?

Last Updated : 23 Jul, 2024
Suggest changes
Share
Like Article
Like
Report

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:

Approach 1: Direct Addition of Seconds

Convert 24 hours to seconds and add the result to the current Unix time.

Example: In this example, involves directly adding seconds (24 hours) to the Unix timestamp.

PHP
<?php echo time() + (24*60*60); ?>  

Output
1721795431 

Approach 2: Using strtotime() Function

Since hours in a day vary in systems such as Daylight saving time (DST) from exactly 24 hours in a day. It's better to use PHP strtotime() Function to properly account for these anomalies. Using strtotime() function to parse current DateTime and one day to timestamp.

Example: In this example, leverages the strtotime function to add time.

PHP
<?php echo strtotime("now"), "\n"; echo strtotime('+1 day'); ?> 

Output
1721709116 1721795516

Approach 3: Using DateTime Class

We create a DateTime object with current timestamp and add interval of one day. P1D represents a Period of 1 Day interval to be added.

Example: In this example, uses PHP's DateTime and DateInterval classes, which offer more flexibility and readability.

PHP
<?php // Get current time stamp $now = new DateTime(); $now->format('Y-m-d H:i:s'); echo $now->getTimestamp(), "\n"; // Add interval of P1D or Period of 1 Day $now->add(new DateInterval('P1D')); echo $now->getTimestamp(); ?> 

Output
1721709230 1721795630

Next Article

Similar Reads