How to tale only hour from the date time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    How to tale only hour from the date time

    I want to take only the hour from the date time field. For that first i apply the explode.[PHP]$Query1 = explode(" ",$row['Date']);[/PHP]
    output:13:30:00 13:30:0016:20:0 016:20:00(This include hour:minutes:se conds)
    And now i want to extract only first 2 digit(hour).
    ex:13,13,16,16
    HOw can i do that? Please help me.
  • Brosert
    New Member
    • Jul 2008
    • 57

    #2
    try
    Code:
    date('h',$row['Date']);
    if the sql stores the date as a single int....
    if the Date field is a String representation, you may like to first convert to a time object, using:
    Code:
    $temp=strtotime($row['Date']);
    $hour=date('h',$temp);
    note: h will give you the hour in 12 hr format - H will give 24hr format

    you could also try to extract the hour yourself by manipulating the string.....

    Comment

    • ghjk
      Contributor
      • Jan 2008
      • 250

      #3
      Originally posted by Brosert
      try
      Code:
      date('h',$row['Date']);
      if the sql stores the date as a single int....
      if the Date field is a String representation, you may like to first convert to a time object, using:
      Code:
      $temp=strtotime($row['Date']);
      $hour=date('h',$temp);
      note: h will give you the hour in 12 hr format - H will give 24hr format

      you could also try to extract the hour yourself by manipulating the string.....

      http://us3.php.net/date




      I did it. But got only 20. This is my code.[PHP]
      $Date= "13:30:0013:30: 0016:20:0016:20 :00";
      date('H',$row['Date']);

      $temp=strtotime ($row['Date']);
      $hour=date('H', $temp);
      echo "$hour";[/PHP]

      Comment

      • Brosert
        New Member
        • Jul 2008
        • 57

        #4
        Why are there 4 seperate Times in the field??

        I think you will have to manipulate the string yourself....
        firtunately, the field should always be the same size. You can use the php substr method which takes string, offset and lengh as arguments....eg , the first hour would be
        Code:
        $hr1=substr($Date,0,2);
        the second would be
        Code:
        $h2=substr($Date8,2);
        etc...
        If you wanted to, you could store them all in an Array - and use a loop to calculate them....

        Comment

        Working...