Solved

add and subtract day/week/month/year from unix time?

Posted on 2004-09-17
4
450 Views
Last Modified: 2010-05-18
hi, topic says it all except that it must compensate for DST and such other things...

perfect answer would be 2 functions (add & sub) which take a unix timestamp and a value defining what unit (day, week, month, year)

i've tried alot of things myself, nothing seems to be perfect :)
0
Comment
Question by:clocKwize
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12089122
Heres a basic function which covers both, il explain it in a follow up post

function altertime($stamp,$mode,$quantity,$unit) {
 $stamp = strtotime($mode.$quantity.$unit,$stamp);
 return $stamp;
}
0
 
LVL 48

Expert Comment

by:hernst42
ID: 12089123
Have you looked at the strtotime function ( http://de3.php.net/manual/en/function.strtotime.php )
e.g

echo strtotime("+1 day"), "\n";
actual-unix timestamp +1 day

echo strtotime("+1 week", $unixtimestamp), "\n";
$unixtimestamp +1 week
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 250 total points
ID: 12089132
the function is called altertime

the parameters are

$stamp - the timestamp
$mode - this is add "+" or sub "-"
$quantity - the number of days,weeks,months or years to add or subtract
$unit - "day" "week" "month" or "year"

example

function altertime($stamp,$mode,$quantity,$unit) {
 $stamp = strtotime($mode.$quantity.$unit,$stamp);
 return $stamp;
}

$time = time();
$newtime = altertime($time,"-",1,"day");
echo date("d m Y",$time);
echo "<br>";
echo date("d m Y",$newtime);
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12089145
heres a few other examples showing calling the function

$newtime = altertime($time,"-",10,"week"); //will return a timestamp 10 weeks before
$newtime = altertime($time,"+",1,"year"); //will return a timestamp 1 year after
$newtime = altertime($time,"+",2,"month"); //will return a timestamp 2 months after
$newtime = altertime($time,"-",2,"day"); //will return a timestamp 2 days before
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question