Results 1 to 6 of 6

Thread: Plus or minus hours

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Plus or minus hours

    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

  2. #2
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    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

    $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

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Does anyone know of a shorter way to do this or is this about it?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I found a better way. Just convert it to a timestamp and subtract or add 3600 (which is equal to an hour)
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259
    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")));

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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:

    Code:
    $time = date("G:i A", strtotime(date("G:i A")) - 7200);
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width