1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.
The time () function generates a long string of numbers representing how many seconds have passed since January 1, 1970. It is possible to add and subtract time by adding and subtracting seconds.

Jody asks:
How do I offset the time for my time zone? I am central and my server time is an hour ahead of me.

Well one way is to simply subtract an hour (or to be more exact 3600 seconds.) Here is an example:

<?php
$b = time () - 3600;
print date("D, F jS G:i a",$b) . "<br>";
?>

You can add or subtract any amount of seconds to the time function while you create it's variable. This let's you offset minutes, hours, days or even years.

More about Time and Calendars

Comments
September 12, 2007 at 6:36 pm
(1) Jody says:

That didn’t help me figure out what code to add to the cookie code you provided. I just simply want to know what to put in this code and where to put it in this code to adjust the time the cookies reads before it sets the cookie:

?php
$Month = 31536000 + time() ;
//this adds 365 days to the current time
setcookie(JodySite1, date(F jS, Y @ g:i A), $Month);
?

That’s the code you provided in your PHP Cookie tutorial at
http://php.about.com/od/advancedphp/qt/php_cookie.htm

That code reads the server time and puts it in the cookie but I want it to read the server time, subtract an hour and THEN put it in the cookie.

Maybe I’m just not being clear enough.

I know the code you pasted today to just do a simple time read, subtract an hour and echo it to the presented web page. But that’s totally different than what I am asking here.

I guess I am just going to remove your cookie code since I can’t get it to subtract an hour from server time before it writes the cookie to a visitor’s browser.

Thanks.

Jody

September 30, 2008 at 4:28 pm
(2) Chyann says:

I have always had a problem with adding and subtracting time like for an example:5h.34min.
+2h.46min.
———-
I just can’t find a way to help me click with it.

January 16, 2009 at 10:13 am
(3) Dave says:

chill out Jody.

February 28, 2010 at 3:00 pm
(4) Mitchel Haas says:

Clear, concise, and correct.

Thanks!

March 13, 2011 at 1:14 am
(5) Peter says:

Since about two days ago when I add 17 hours to a timestamp obtained by using the time() function on a server where DST is 0, and display it using the date() function, it displays a time that is 18 hours ahead as if DST is 1. Any ideas why this is suddenly happening now??

March 13, 2011 at 4:11 am
(6) Peter says:

Ignore the above comment. Looks like it is only a problem when the server is going through a daylight saving changeover. i.e. the first time was with DST = 0, but by adding 17 hours the new time was in a date with DST = 1 causing the issue. Once the server has gone into DST it is all correct again.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.