Php Problem to get hour & minutes from smalldatetime types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaif
    New Member
    • Mar 2008
    • 24

    Php Problem to get hour & minutes from smalldatetime types

    Hi all, I am working on a module of Php project with SQL server. I design a date filed in the table smalldatetime types. I want to get Hour and minutes. I write the code bellow: Its works for hour but not for minutes. pls give me idea if diff way ...... many thx
    [code=php]
    $S= gmdate("H", strtotime($row[2]));
    $Sm= gmdate("m", strtotime($row[2]));[/code]
    Last edited by pbmods; Jul 23 '08, 01:43 AM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Shaif.

    'm' fetches the day of the month with leading zero. You want 'i' (http://php.net/date).

    Also, for performance, consider doing something like this so that you only have to call strtotime() once:
    [code=php]
    $timeRow = strtotime($row[2]);
    $S= gmdate("H", $timeRow);
    $Sm= gmdate("m", $timeRow);
    [/code]

    Comment

    • shaif
      New Member
      • Mar 2008
      • 24

      #3
      Originally posted by pbmods
      Heya, Shaif.

      'm' fetches the day of the month with leading zero. You want 'i' (http://php.net/date).

      Also, for performance, consider doing something like this so that you only have to call strtotime() once:
      [code=php]
      $timeRow = strtotime($row[2]);
      $S= gmdate("H", $timeRow);
      $Sm= gmdate("i", $timeRow);
      [/code]

      Hi Pbmods, Ya its working how ever , its get only rounded minutes such as 30 mins only if have. But if 00.09 , its cant count. Is there any way to get exact minutes pls?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        You want to round to the nearest 30 minutes?

        [code=php]
        $minutes = ((int) ($minutes / 30 + .5)) * 30;
        [/code]

        Comment

        Working...