Solved

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

Posted on 2004-09-17
4
403 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now