简体   繁体   English

Codeigniter仅在更新和插入时挂接

[英]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). 更新或将日期插入数据库时​​,我需要将日期格式从用户的显示方式(“ dd-mm-yyyy”)更改为mysql格式(“ yyyy-mm-dd”)(php-codeigniter为正在使用)。 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. 已经有很多地方需要此修复程序,因此我认为可能可以用挂钩将每个“ 2digit-2digit-4digit”更改为相反的格式。

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. 另外,您无需担心mysql数据类型。 What you need is just load date helper and perform mdate() before display the date in your view, something like... 您需要的只是加载日期帮助程序并在视图中显示日期之前执行mdate(),例如...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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