Open In App

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

Improve
Improve
Like Article
Like
Save
Share
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:

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

  • Program:




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

    
    

  • Output:
    1588671070

Method 2: 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 to parse current DateTime and one day to timestamp

  • Program:




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

    
    

  • Output:
    1588584696
    1588671096

Method 3: Using DateTime class we can achieve same result. First create a DateTime object with current timestamp and add interval of one day. P1D represents a Period of 1 Day interval to be added.

  • Program:




    <?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:
    1588584729
    1588671129


Last Updated : 17 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads