Date Problem with PHP!

Discussion in 'PHP' started by sumitt_2004, Jun 14, 2009.

  1. #1
    Hi,

    Could anyone please help me in accessing Date through 3 drop down boxes (DD/MM/YYYY)? I took 3 list box for Date of Birth MM,DD,and YYYY respectively but unable to store this data into MySQL table in DATE format. Right now, I am using this to store date of birth in database.
    $day=$_POST['day'];
    $month=$_POST['month'];
    $year=$_POST['year'];
    $dob=$day.'/'.$month.'/'.$year;

    Could anyone please tell me where I am wrong?

    Regards,
    Sumit Kumar
     
    sumitt_2004, Jun 14, 2009 IP
  2. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #2
    krishmk, Jun 14, 2009 IP
  3. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I wrote this function to convert a date collected by a date picker on the form to a mysql datetime format.
    you can play around with it to fit your needs.

    
    //My datepicker string:Jun 05 2009, 11:54 PM
    	function date_picker_to_mysql($date){
    			
    		$am_pm = substr($date, -2);
    		$minutes = substr($date, -5, -3);
    		$hours = substr($date, -8, -6);
    		$year = substr($date, -14, -10 );
    		$day = substr($date, -17, -15);
    		
    		if(string_contains($date, "Dec"))
    			$month = "12";
    		else
    		if(string_contains($date, "Nov"))
    			$month = "11";	
    		else
    		if(string_contains($date, "Oct"))
    			$month = "10";	
    		else
    		if(string_contains($date, "Sep"))
    			$month = "09";
    		else
    		if(string_contains($date, "Aug"))
    			$month = "08";								
    		else
    		if(string_contains($date, "Jul"))
    			$month = "07";				
    		else
    		if(string_contains($date, "Jun"))
    			$month = "06";
    		else
    		if(string_contains($date, "May"))
    			$month = "05";
    		else
    		if(string_contains($date, "Apr"))
    			$month = "04";
    		else
    		if(string_contains($date, "Mar"))
    			$month = "03";
    		else
    		if(string_contains($date, "Feb"))
    			$month = "02";
    		else
    		if(string_contains($date, "Jan"))
    			$month = "01";	
    			
    		//convert $12hrtime to 24 hour time
    		$hrtime = $hours.":".$minutes." ".$am_pm;
    		$sqlhrtime = date("G:i",strtotime($hrtime)); // Ex. 18:36
    		if(strlen($sqlhrtime) == 4){
    			$sqlhrtime = "0".$sqlhrtime;
    		}
    									
    		return $year."-".$month."-".$day." ".$sqlhrtime.":00";
    		
    	}
    PHP:
    enjoy and good luck:)
     
    zeronese, Jun 21, 2009 IP
  4. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #4
    Whipped up even shorter code. Does the exact same thing :)
    
    	function strtodate($date)
    	{		
    		return date("m-d-Y G:i A", strtotime($date));
    	}
    	
    	print strtodate("January 05 2009, 11:54 PM");
    
    PHP:
    I think krishmk's method is best though.
     
    Louis11, Jun 22, 2009 IP
  5. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    to have a valid mysql timestamp your function should be:
    function strtodate($date) {
    return date("Y-m-d H:i:s", strtotime($date));
    }

    i meant with my function to have a general procedure that can be manipulated according to a datepicker. Many people have this problem when comming to use a datepicker, the strtotime give them strange results if the $date string is not formated right.
    for example: for 1-5-2009, the strtotime will say this is january 5th, 2009... but the datepicker could be giving this as may first in some countries.

    if the string is formated right, i will sure use your function (with the tiny fix i added) :)
     
    zeronese, Jun 22, 2009 IP
  6. findonline

    findonline Peon

    Messages:
    149
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I ran into this too the other day, its usually best practice to store all your dates in MySQL as date format (YYYY-MM-DD) and then use PHP to convert them to another format. Then you can compare dates and use the NOW() function and other date functions in your MySQL queries.

    -----------------------
    Find Online Schools
     
    findonline, Jun 22, 2009 IP
  7. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I agree 100%, otherwise just store it as a varchar, but then it is not useful for any mysql queries. not to mention the flexebility of formating using the php date function :)
     
    zeronese, Jun 22, 2009 IP
  8. uselessguy

    uselessguy Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    should be YYYY-MM-DD
     
    uselessguy, Jun 22, 2009 IP