1. Home
  2. Computing & Technology
  3. PHP / MySQL
photo of Angela Bradley

Angela's PHP / MySQL Blog

By Angela Bradley, About.com Guide to PHP / MySQL

Adding and Subtracting Time

Wednesday September 12, 2007
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.

Leave a Comment

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

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL

©2008 About.com, a part of The New York Times Company.

All rights reserved.