Best way to subtract 2 hours from datetime expresion?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gertrudis
Forum Newbie
Posts: 14
Joined: Wed Jan 27, 2010 8:15 pm

Best way to subtract 2 hours from datetime expresion?

Post by gertrudis »

Hi to all, I need to know how to subtract hour from date expression
command?

example: I want to sutract 2 hour to a time?
$acttime =date("H:m:s")-2;

thanks in advance!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Best way to subtract 2 hours from datetime expresion?

Post by AbraCadaver »

Code: Select all

$acttime = date("H:i:s", strtotime("-2 hours"));
Last edited by AbraCadaver on Thu Jan 28, 2010 9:16 am, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
gertrudis
Forum Newbie
Posts: 14
Joined: Wed Jan 27, 2010 8:15 pm

Re: Best way to subtract 2 hours from datetime expresion?

Post by gertrudis »

Thanks a lot
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Best way to subtract 2 hours from datetime expresion?

Post by AbraCadaver »

Well I guess if you already have a timestamp, this works as well:

Code: Select all

$acttime = date("H:i:s", time()-7200);  //subtract 7200 seconds
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: Best way to subtract 2 hours from datetime expresion?

Post by akuji36 »

Use php offset

Ex

Code: Select all

<?php echo date("l"); ?>&nbsp&nbsp
<?php echo date("F j, Y"); ?>&nbsp&nbsp
<?php
$offset = strtotime("-5 hours");
echo date("h:i a",  $offset);
?>
 
Using phpdate function and php offset.
This example gives the current time (eastern time)
- 5 hours. My server is five hours ahead. In order to return
eastern standard time I use this function.

thanks

Rod
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Best way to subtract 2 hours from datetime expresion?

Post by AbraCadaver »

Redundant much?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Best way to subtract 2 hours from datetime expresion?

Post by pickle »

AbraCadaver wrote:Well I guess if you already have a timestamp, this works as well:

Code: Select all

$acttime = date("H:i:s", time()-7200);  //subtract 7200 seconds
Stick with strtotime(), that could cause problems during a Standard Time / Daylight Saving Time crossover.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Best way to subtract 2 hours from datetime expresion?

Post by AbraCadaver »

pickle wrote:
AbraCadaver wrote:Well I guess if you already have a timestamp, this works as well:

Code: Select all

$acttime = date("H:i:s", time()-7200);  //subtract 7200 seconds
Stick with strtotime(), that could cause problems during a Standard Time / Daylight Saving Time crossover.
Good thought.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply