php 在活动记录 Codeigniter 中将日期格式显示为 DD MM YYYY

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35922791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:37:50  来源:igfitidea点击:

Display date format as DD MM YYYY in active record Codeigniter

phpcodeigniterdatedatetime

提问by Beep

How would I alter my active record to display my date as DD MM YYYY rather than YYYY MM DD?

我将如何更改我的活动记录以将我的日期显示为 DD MM YYYY 而不是 YYYY MM DD?

Current Display

当前显示

2016-01-07

2016-01-07

Aim Display

目标显示

07-01-2016

07-01-2016

CurrentView

当前视图

<?php echo $row->start_date; ?>

TriedView

试过的视图

<?php echo $row->DATE_FORMAT(start_date, '%d/%m/%Y'); ?>

Model

模型

class Profiles_model extends CI_Model

    {

        function get_bank()
        {
            $this->db->where('mem_id', $this->session->userdata("id"));
            $query = $this->db->get('bank');
            return $query->result();
        }

    }

回答by Drudge Rajen

You can use strtotime()and date()of php as :

您可以将php 的strtotime()date()用作:

$originalDate = $row->start_date;
$newDate = date("d-m-Y", strtotime($originalDate));

回答by Avnish alok

I always prefer to make a helper function in Codeigniter for date formatting because date is very common to use on many pages.

我总是喜欢在 Codeigniter 中创建一个用于日期格式的辅助函数,因为日期在许多页面上使用非常普遍。

Codeigniter 3 helper function:

Codeigniter 3 辅助函数:

if(!function_exists('dateFormat'))
{
    function dateFormat($format='d-m-Y', $givenDate=null)
    {
        return date($format, strtotime($givenDate));
    }
}

Autoload this helper in your Codeigniter autoload.php and then use it freely anywhere in controller/model/view.

在你的 Codeigniter autoload.php 中自动加载这个助手,然后在控制器/模型/视图中的任何地方自由使用它。

use in view like:

在视图中使用,例如:

<?= dateFormat('d-m-Y',$info->createdDtm) ?>