简体   繁体   中英

Codeigniter hook only on updates and inserts

I need to change the format of dates from how it appears to the user ("dd-mm-yyyy") to the mysql format ("yyyy-mm-dd") when updating or inserting them to the database (php - codeigniter is being used). There's already a lot of place needing this fix so I thought may be I could do it with the hooks and changing every "2digit-2digit-4digit" to the reversed format.

So is there a way to create a hook that can work only on update and insert data (not to scan every single bit) and is there any other way of doing it without having to place a reverse function call everywhere I have dates inserted - updated?

You doesn't need to use hook here, hook is needed when you need to overide the Core behaviour. Also you doesn't need to worry about your mysql datatypes. What you need is just load date helper and perform mdate() before display the date in your view, something like...

$this->load->helper('date');
// assume you hold date (retrieved from your database) in $date variable
$date = mdate('%d-%m-%Y', $date);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM