Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now