Welcome Guest, Not a member yet? Register   Sign In
Trouble with DATE field and date/strtotime conversion
#1

[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.
#2

[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’ ] ) );
#3

[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
#4

[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' ] );




Theme © iAndrew 2016 - Forum software by © MyBB