PHP: add seconds to a date

前端 未结 9 1460
庸人自扰
庸人自扰 2020-12-04 01:22

I have $adate; which contains:

Tue Jan 4 07:59:59 2011

I want to add to this date the following:

$duration=674         


        
相关标签:
9条回答
  • 2020-12-04 02:01

    If you are using php 5.3+ you can use a new way to do it.

    <?php 
    $date = new DateTime();
    echo $date->getTimestamp(). "<br>";
    $date->add(new DateInterval('PT674165S')); // adds 674165 secs
    echo $date->getTimestamp();
    ?>
    
    0 讨论(0)
  • 2020-12-04 02:07

    I have trouble with strtotime() to resolve my problem of add dynamic data/time value in the current time

    This was my solution:

    $expires = 3600; //my dynamic time variable (static representation here)
    $date = date_create(date('Y-m-d H:i:s')); //create a date/time variable (with the specified format - create your format, see (1))
    echo date_format($date, 'Y-m-d H:i:s')."<br/>"; //shows the date/time variable without add seconds/time
    date_add($date, date_interval_create_from_date_string($expires.' seconds')); //add dynamic quantity of seconds to data/time variable
    echo date_format($date, 'Y-m-d H:i:s'); //shows the new data/time value
    

    font: https://secure.php.net/manual/en/datetime.add.php (consult Object Oriented style too, the Elzo Valugi solution)

    (1) https://secure.php.net/manual/en/function.date.php

    0 讨论(0)
  • 2020-12-04 02:08
    $current_time_zone = 150;
    date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s"))+$current_time_zone);
    
    0 讨论(0)
提交回复
热议问题