PDA

Click to See Complete Forum and Search --> : Plus or minus hours


prokhaled
Aug 8th, 2002, 09:13 AM
I use this code to print time

$Time=date("G:i A");
echo $Time;

output (for example):
20:51 PM

the problem that hour in my country is 18:51 PM .. so how can I minus 2 hour from day.

Thanks

TheGoldenShogun
Aug 8th, 2002, 09:59 AM
Its kinda tricky but off hand this is all I can think of.

$Time=date("G:i A");

//substring out the hour and minute from your inital function

$TimeToken = explode(":", $Time);

$subHour = $TimeToken[0];
$subMinute = substr($TimeToken[1], 0, 2);

//add it to the mktime function -2 on the hour
mktime function on php.net (http://www.php.net/manual/en/function.mktime.php)

$makeTime = mktime ( $subHour -2,$subMinute,0,1,1,2002);

//use the date function on $makeTime
$grabTime = date("G:i A", $makeTime);
echo("<BR>".$grabTime);

That should do it the long way... you might want to check out the localtime function too... that may be where you want to go

localtime function on php.net (http://www.php.net/manual/en/function.localtime.php)

The Hobo
Aug 19th, 2002, 03:22 PM
Does anyone know of a shorter way to do this or is this about it?

The Hobo
Aug 19th, 2002, 03:59 PM
I found a better way. Just convert it to a timestamp and subtract or add 3600 (which is equal to an hour)

prokhaled
Aug 20th, 2002, 04:49 AM
And I found very Better way

use that:

$GMT="-2";
echo date ("G:i A",mktime (date("G")+$GMT,date("i"),date("s"),date("m"),date("d"),date("Y")));

The Hobo
Aug 20th, 2002, 09:31 AM
Originally posted by prokhaled
And I found very Better way

use that:

$GMT="-2";
echo date ("G:i A",mktime (date("G")+$GMT,date("i"),date("s"),date("m"),date("d"),date("Y")));

For some reason, I think this way is much prettier:


$time = date("G:i A", strtotime(date("G:i A")) - 7200);