Solved

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

Posted on 2004-09-17
4
435 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 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