Welcome Guest, Not a member yet? Register   Sign In
A "User's Guide" to Jamie Rumbelow's MY_Model library
#8

[eluser]theshiftexchange[/eluser]
I wanted a better solution to mySQL date conversions. In my application, dates are "DD-MM-YYYY" due to the local area. But in mySQL they are "YYYY-MM-DD". So I used callbacks to always flip the date prior to retrial and storage of dates.

Code:
class Fake_model extends MY_Model
{

public $before_update = array ('date_convert_to_mysql');
public $before_create = array ('date_convert_to_mysql');
public $after_get = array ('date_convert_to_php');


// Make all dates in DD-MM-YYYY format for our app (from YYYY-MM-DD)
protected function date_convert_to_php($result)
{
  $result->purchase_date = date("d-m-Y", strtotime($result->purchase_date));
  return $result;
}

// Make all dates in YYYY-MM-DD format for mySQL (from DD-MM-YYYY)
protected function date_convert_to_php($post)
{
  $post['purchase_date'] = date("Y-m-D", strtotime($post['purchase_date']));
  return $post;
}
}

Works perfectly for me for single inserts/updates and single/multiple gets. No more date format issues!!!

(Havent tested for multiple inserts/updates)


Messages In This Thread
A "User's Guide" to Jamie Rumbelow's MY_Model library - by El Forum - 01-13-2012, 11:36 PM
A "User's Guide" to Jamie Rumbelow's MY_Model library - by El Forum - 03-01-2012, 11:53 AM
A "User's Guide" to Jamie Rumbelow's MY_Model library - by El Forum - 03-01-2012, 12:09 PM
A "User's Guide" to Jamie Rumbelow's MY_Model library - by El Forum - 03-01-2012, 12:30 PM
A "User's Guide" to Jamie Rumbelow's MY_Model library - by El Forum - 03-01-2012, 01:33 PM
A "User's Guide" to Jamie Rumbelow's MY_Model library - by El Forum - 04-24-2012, 11:24 PM
A "User's Guide" to Jamie Rumbelow's MY_Model library - by El Forum - 04-25-2012, 12:02 AM
A "User's Guide" to Jamie Rumbelow's MY_Model library - by El Forum - 05-03-2012, 09:52 PM
A "User's Guide" to Jamie Rumbelow's MY_Model library - by El Forum - 05-03-2012, 10:04 PM



Theme © iAndrew 2016 - Forum software by © MyBB