Philip Kirkbride
Philip Kirkbride

Reputation: 22859

Looking to 'shift' time with PHP?

I want to be able to shift time if a $_GET['date'] is set.

Here is the code.

if(isset($_GET['date']) && $_GET['date'] !="") {
$date =time ();
}else {
$date =time ();
}

I'd like to be able to feed a year & month value through date and still have date equal time but with the time value shifted to the appropriate month and year.

What is the best what to go about this?

Upvotes: 1

Views: 1797

Answers (2)

Andreas
Andreas

Reputation: 2266

I would recommend deconstructing the timestamp and then use mktime(...) to reconstruct it with the "fields" of your choosing. You can extract individual "fields" with getdate() or even with date().

$f = getdate($timestamp);
$newtimestamp = mktime($f["hours"], $f["minutes"], $f["seconds"], $month, $day, $year);

Upvotes: 3

Brad Christie
Brad Christie

Reputation: 101604

strtotime possibly?

$time = time()
--or--
$time = strtotime('+1 year', time());

Though I'm not sure I understand the question with how you've worded it.

Upvotes: 2

Related Questions