SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    seattle
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need to convert standard time dropdowns to military time before insert

    i have 3 dropdowns. 1 for hour, minute and am/pm. i need to convert it to military time before i insert it into mysql time field. can someone help me modify this so it works? currently if the user selects 12:00 am it inserts 12:00:00 and if they select 12:00 pm it inserts 24:00:00.

    Code:
    $h = split(":", $results['start']);
    	 $m = split(" ", $h[1]);
    	 
    	 $hour = ($h[0] > 12 ? (int)($h[0]-12) : (int)$h[0]);
    	 $minute = (int)$m[0];
    	 $ampm = ($h[0] > 12 ? "pm" : "am");
    
    	 $hours = "";
    	 for ($n=0;$n<=12;$n++)
    	 	if ($hour == $n)
    			$hours .= "<option selected='selected'>".$n."</option>";
    		else
    			$hours .= "<option>".$n."</option>";
    	 
    	 $minutes = "";
    	 for ($n=0;$n<=55;$n += 5)
    		$minutes .= "<option ".($minute==$n ? "selected" : "").">".$n."</option>";
    	
    	 $ampms = "<option ".($ampm=="am" ? "selected" : "").">am</option><option ".($ampm=="pm" ? "selected" : "").">pm</option>";
    
    	 $h = split(":", $results['finish']);
    	 $m = split(" ", $h[1]);
    	 
    	 $hour = ($h[0] > 12 ? (int)($h[0]-12) : (int)$h[0]);
    	 $minute = (int)$m[0];
    	 $ampm = ($h[0] > 12 ? "pm" : "am");
    
     Start: <select name="start_hour[]">'.$hours.'</select> <select name="start_minutes[]">'.$minutes.'</select> <select name="start_ampm[]">'.$ampms.'</select> 
    
    
    // insert statement
    
          for ($i = 0; $i < count($_POST['assignto']); $i++)
             {
                
                    $start = ($_POST['start_ampm'][$i] == "pm" ? $_POST['start_hour'][$i]+12 : $_POST['start_hour'][$i]).":".$_POST['start_minutes'][$i];
                 
                    $finish = ($_POST['finish_ampm'][$i] == "pm" ? $_POST['finish_hour'][$i]+12 : $_POST['finish_hour'][$i]).":".$_POST['finish_minutes'][$i];
              
                    
                    $sql = "INSERT INTO workassign (mid, assignto, start, finish) VALUES ('$id', '".$_POST['assignto'][$i]."', '".$start."', '".$finish."')";
                    $result = mysql_query($sql) or die("Cannot perform timework query: " . mysql_error());
            }

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    // $hour is a value 1 to 12
    
    $24format_hour = $hour &#37; 12;
    if ($pm) {
        $24format_hour += 12;
    }
    Generally, 24 is considered an invalid value because it would be the start of the next day.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Careful with crmalibu's code, as variables can't start with a number

  4. #4
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    seattle
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how would i modify my insert code to work with your suggestion?

  5. #5
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    seattle
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so i modified this code and it works for the $start, but not $finish. no matter what time i try it enters it as 16:00 into the db. when i print_r on $_POST this is what it gives me:

    Array ( [id] => 530 [item] => Electric door lock [mdate] => 03/24/2009 [wid] => Array ( [0] => 299 [1] => 298 ) [assignto] => Array ( [0] => 4 [1] => 5 ) [start_hour] => Array ( [0] => 11 [1] => 12 ) [start_minutes] => Array ( [0] => 30 [1] => 30 ) [start_ampm] => Array ( [0] => am [1] => pm ) [finish_hour] => Array ( [0] => 12 [1] => 1 ) [finish_minutes] => Array ( [0] => 0 [1] => 0 ) [finish_ampm] => Array ( [0] => pm [1] => pm ) [totaltime] => 8.00 [tid] => 1 [description] => provide 120v for Chase glass to the new door [status] => [cost] => 0.00 [descript] => [note] => [submit] => UPDATE RECORD )

    which is correct so it has something to do the the $finish variable on the conversion, but i can't figure it out...can someone help please?

    Code:
    for ($i = 0; $i < count($_POST['assignto']); $i++)
             {
    $start = $_POST['start_hour'][$i].":".$_POST['start_minutes'][$i]." ".$_POST['start_ampm'][$i];
    $start = date("H:i",strtotime($start));
    //print_r ($_POST['finish_hour'][$i]);
    //print_r ($_POST['finish_minutes'][$i]);
    //print_r ($_POST['finish_ampm'][$i]);
     
    $finish = $_POST['finish_hour'][$i].":".$_POST['finish_minutes'][$i]." ".$_POST['finish_ampm'][$i];
    $finish = date("H:i",strtotime($finish));
    //print_r ($finish);
     
     
       $sql = "INSERT INTO workassign (mid, assignto, start, finish) VALUES ('$id', '".$_POST['assignto'][$i]."', '".$start."', '".$finish."')";
                    $result = mysql_query($sql) or die("Cannot perform timework query: " . mysql_error());
            }
            
    print_r ($_POST);
          header("Location: view.php");
              exit();
       }

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Maybe I'm missing something here?
    PHP Code:
    <?php
    echo date(
        
    'H:i:s',
        
    strtotime(
            
    sprintf(
                
    '%s:%s:%s%s',
                
    '12',    #hour
                
    '35',    #minute
                
    '06',    #second
                
    'am'    #Ante/Post meridiem
            
    )
        )
    ); 
    #00:35:06
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    seattle
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea, but wouldn't this work the same way?
    $finish = $_POST['finish_hour'][$i].":".$_POST['finish_minutes'][$i]." ".$_POST['finish_ampm'][$i];
    $finish = date("H:i",strtotime($finish));

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I would say the fact you're passing 1:0pm to strtotime is making it fail, try zero-padding the values.
    PHP Code:
    <?php
    echo date(
        
    'H:i:s',
        
    strtotime(
            
    sprintf(
                
    '%02d:%02d:00 %s',
                
    '1',    #hour
                
    '0',    #minute
                
    'pm'    #Ante/Post meridiem
            
    )
        )
    );    
    #13:00:00
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    seattle
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks i got it...i put on on my dropdowns instead.


Bookmarks

Posting Permissions

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