nickelus Posted August 6, 2009 Share Posted August 6, 2009 Hi folks, I'm having a hell of a time reordering my date format. I'm using a JavaScript date picker calendar on in my form to post data. the date picker likes to see the m-d-Y format otherwise it cant define the days or months properly. so i use date_format() in the query and all is fine. however when i format the date to Y-m-d & update the records in the db the date is not correct(non well formed) in a troubleshooting script Ive done this to isolate the issue. Form: <?php include db.php; mysql_select_db($database_bio); $ctrct="SELECT client_id,date_format(start_date,'%m-%d-%Y') AS sd , date_format(end_date,'%m-%d-%Y') AS ed,term_length,drum_count,drum_amt_paid,price_per,contract_id,freq,contract_status FROM `contract` WHERE contract_id=$contract_id"; $ctrct_qry=mysql_query($ctrct); while($row=mysql_fetch_array($ctrct_qry)){ $client_id=$row[0]; $start_date=$row['sd']; $end_date=$row['ed']; $term_length=$row[3]; $drum_count=$row[4]; $drum_amt_paid=$row[5]; $price_per=$row[6]; $contract_id=$row[7]; $freq=$row[8]; $contract_status=$row[9];?> <form name="contract" method="POST" action="<?php echo "test_dates.php";?>"> <table align="center"> <tr> <td align="center"><strong>Start date</strong></td> <td align="center"><input name="start_date" type="text" value="<?php if(isset($start_date)){ echo date("m-d-Y",strtotime($start_date)); } ?>" onFocus="showCalendarControl(this);"/> </td> </tr><tr> <td align="center"><strong>End date</strong></td> <td align="center"><input name="end_date" type="text" value="<?php if(isset($end_date)){ echo date("m-d-Y",strtotime($end_date)); } ?>" onFocus="showCalendarControl(this);"/></td> </tr></table></form> Testing: <?php $sDate=$_POST['start_date']; $eDate=$_POST['end_date']; echo $sDate."<br/>"; echo $eDate."<br/>"; $s=date('Y-m-d',$sDate); $e=date('Y-m-d',$eDate); echo $s."<br/>"; echo $e."<br/>";?> and what i get back from $s and $e is 1969-12-31 or non well formed. i've also tried $s=date("Y-m-d",strtotime($sDate)) in place of just plain date() Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted August 6, 2009 Share Posted August 6, 2009 I think you need to use strtotime to convert to a unix timestamp $s=date('Y-m-d',strtotime($sDate)); $e=date('Y-m-d',strtotime($eDate)); Quote Link to comment Share on other sites More sharing options...
nickelus Posted August 6, 2009 Author Share Posted August 6, 2009 yeah strtotime() is the first way i tried but with no success. So then i figured maybe since the data was already formatted as a date i would try without strtotime() same results Quote Link to comment Share on other sites More sharing options...
nickelus Posted August 6, 2009 Author Share Posted August 6, 2009 somehow its mixing up the date and month Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 6, 2009 Share Posted August 6, 2009 It appears strtotime() is not able to handle that format, so returns false. That causes date() to return the earliest possible time it is able to handle, which is the 1969 date. There's a handful of ways you can do this: 1) regexp, simple to implement but may be computationally expensive to invoke the regexp engine for a small task 2) explode(), array_shift(), and string concatenation, may be overkill on memory but simple to implement 3) repeated calls to strpos() 4) for loop Here is a solution using strtok(): <?php header( 'Content-Type: text/plain' ); $dates = array(); $dates[] = '12-31-2009'; // dec. 31st $dates[] = '7-4-2008'; // july 4th $dates[] = '1-5-2008'; // jan 5th foreach( $dates as $date ) { echo "{$date} yields: " . dateConvert_mdy2ymd( $date ) . "\n"; } function dateConvert_mdy2ymd( $date ) { $m = sprintf( "%02d", (int)strtok( $date, '-' ) ); $d = sprintf( "%02d", (int)strtok( '-' ) ); $y = sprintf( "%04d", (int)strtok( '-' ) ); return "{$y}-{$m}-{$d}"; } ?> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 6, 2009 Share Posted August 6, 2009 Here's one that's a tight pass through the string with no external function calls. Could possibly be optimized but I must get back to work! function dateConvert_mdy2ymd( $date ) { $date .= '-'; $ymd = ''; $part = ''; $char_count = 0; $hyphens = 0; for( $i = 0; true; $i++ ) { if( $date[$i] == '' ) { break; } if( $date[$i] == '-' ) { $len = $hyphens < 2 ? 2 : 4; // m, d should be len 2, y should be len 4 while( $char_count < $len ) { $part = '0' . $part; $char_count++; } if( $hyphens === 0 ) { $ymd = $part; }else if( $hyphens === 1 ) { $ymd .= '-' . $part; }else if( $hyphens === 2 ) { $ymd = $part . '-' . $ymd; } $part = ''; $char_count = 0; $hyphens++; continue; } $part .= $date[$i]; $char_count++; } return $ymd; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.