JPollock
JPollock

Reputation: 3558

How to increase the value of a unix timestamp stored in a variable by X hours?

I have a php function that creates a unix timestamp and stores it in a variable. Let's call that variable $timestamp. I'm looking for a function that will allow return two new variables that are a each a different number of hours later than the original timestamp. Is there a way to do this?

Upvotes: 0

Views: 309

Answers (2)

Sahil Mittal
Sahil Mittal

Reputation: 20753

Try this-

date( "Y-M-d H:i:s", strtotime( $timestamp ) + $hours * 3600 );

strtotime to convert the timestamp string to timestamp value; then adding the no. of seconds (hours*36*36); then converting back to the timestamp format using date()

So, you can simply add this code to a function, and give any desired values of $hours; to get the-

different number of hours later than the original timestamp

Upvotes: 0

Matthew R.
Matthew R.

Reputation: 4350

This will return a date 3 and 7 days after the supplied date as a unix timestamp. You can edit the math to get the times you need.

$timestamp = date('U');
var_dump(get_more_dates($timestamp));

function get_more_dates($timestamp){

    // 60 seconds * 60 minutes * 24 hours * 3 days
    $new_date_1 = (60*60*24*3)+timestamp;
    $new_date_2 = (60*60*24*7)+timestamp;

    return array( $new_date_1, $new_date_1 );
}

Upvotes: 1

Related Questions