[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-09-17
4
Medium Priority
?
461 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
  • 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 750 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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.
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

864 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