Jump to content

date format change


nickelus

Recommended Posts

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()

Link to comment
Share on other sites

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}";
}
?>

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.