CodeIgniter Forums
Trouble with DATE field and date/strtotime conversion - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Trouble with DATE field and date/strtotime conversion (/showthread.php?tid=44158)



Trouble with DATE field and date/strtotime conversion - El Forum - 08-05-2011

[eluser]elambert[/eluser]
I am having trouble updating a MYSQL DATE field properly.

In the model, I get a retrieve a record with this function:

function get_by_user_id( $id )
{
$this->db->where( 'users_user_id', (int)$id );
$query = $this->db->get( 'profiles' );
if ( $query->num_rows() > 0 )
{
$row = $query->row_array();
$row[ 'profile_date_birth' ] = date( 'm-d-Y', strtotime( $row[ 'profile_date_birth' ] ) );
}
return ( $query->num_rows() > 0 ) ? $row : FALSE;
}

In the model, I update a record with this function:

function update( $id, $data )
{
$data[ 'profile_date_birth' ] = date( 'Y-m-d', strtotime( $data[ 'profile_date_birth' ] ) );
$this->db->where( 'profile_id', (int)$id );
$query = $this->db->update( 'profiles', $data );
return ( $query ) ? TRUE : FALSE;
}

So the profile_date_birth field has the value '2000-01-02' saved in the MYSQL DB. It is retrieved and displayed properly on the form as '01-02-2000'. If I save the field without changing it, it comes into the update function correctly as '01-02-2000', but after the date & strtotime conversion, the 'profile_date_birth' is as '2000-02-01' when it should be '2000-01-02'.

Any help would be appreciated.


Trouble with DATE field and date/strtotime conversion - El Forum - 08-05-2011

[eluser]toymachiner62[/eluser]
Please use code tags or don't expect people to help you as it's hard to read code without them.

What is the output when you echo your date function right before
Code:
$data[ ‘profile_date_birth’ ]
like this?

Code:
echo date( ‘Y-m-d’, strtotime( $data[ ‘profile_date_birth’ ] ) );
$data[ ‘profile_date_birth’ ] = date( ‘Y-m-d’, strtotime( $data[ ‘profile_date_birth’ ] ) );



Trouble with DATE field and date/strtotime conversion - El Forum - 08-05-2011

[eluser]elambert[/eluser]
Sorry about that...
Code:
function get_by_user_id( $id )
{
  $this->db->where( ‘users_user_id’, (int)$id );
  $query = $this->db->get( ‘profiles’ );
  if ( $query->num_rows() > 0 )
  {      
    $row = $query->row_array();  
    $row[ ‘profile_date_birth’ ] = date( ‘m-d-Y’, strtotime( $row[ ‘profile_date_birth’ ] ) );        
  }
  return ( $query->num_rows() > 0 ) ? $row : FALSE;
}

In the model, I update a record with this function:

function update( $id, $data )
{
  $data[ ‘profile_date_birth’ ] = date( ‘Y-m-d’, strtotime( $data[ ‘profile_date_birth’ ] ) );              
  $this->db->where( ‘profile_id’, (int)$id );
  $query = $this->db->update( ‘profiles’, $data );        
  return ( $query ) ? TRUE : FALSE;                
}

When I run your code it produces: 2000-02-01


Trouble with DATE field and date/strtotime conversion - El Forum - 08-05-2011

[eluser]elambert[/eluser]
Also, if I add this as the first line in the update function, everthing works fine.
Code:
$data[ 'profile_date_birth' ] = str_replace( '-', '/', $data[ 'profile_date_birth' ] );