Changing server time

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
User avatar
blinddrop
Forum Newbie
Posts: 2
Joined: Wed Oct 19, 2005 5:06 pm
Location: Cochrane & Calgary Alberta

Changing server time

Post by blinddrop »

Hi, I've studied the date() function page on php.net, and searched for a solution, but to no avail, although I reckon there's an easy answer to this question:

How do I subtract 2 hours from the following date timestamp?

$day = date("d ");
$year = date(" Y, H:i:s");


return $month.$day.$year;

The MySQL field is set to a timestamp of NOW(). The server is running PHP4.x so the new PHP 5 function for setting a certain timezone is not possible.

Thanks in advance!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I ran through the halls shouting for joy when I found strtotime(). You can use it like so:

Code: Select all

$timestamp = time();
$less_2_hours = strtotime('-2 hours',$timestamp);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Or just

Code: Select all

$timestamp = time();
$less_two_hours = $timestamp - 6400;
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Jenk wrote:Or just

Code: Select all

$timestamp = time();
$less_two_hours = $timestamp - 6400;
Unless those two hours span a transfer between standard and daylight saving time ;) That's what makes strtotime() so nice - it takes that into consideration.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply