Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:49:09+00:00 2026-06-03T06:49:09+00:00

Scenario: My PHP application will always use the date field DD-MM-YYYY. MySQL obviously always

  • 0

Scenario: My PHP application will always use the date field DD-MM-YYYY. MySQL obviously always stores dates in YYYY-MM-DD

Disclaimer: I know how to manually change the date formats – that is not the question

Question: What is a good, clean, elegant way to ensure that my application always retrives the date in DD-MM-YYYY, and mySQL always receives the date in YYYY-MM-DD to be stored.

i.e. Basically I want to have a single place where the conversion occurs automatically, without me having to call it each and every time I do an SQL query. That way it doesnt matter if I “forget” to do a date_conversion – it will just always happen each and every time….

I’m thinking of some sort of callback somewhere?

(edit) My idea: I currently use the Codeigniter framework. I’m thinking of using MY_Model, using before_get, before_insert and before_update callbacks. In these callbacks, I can go through all the fields using something like this for a SELECT:

// {insert my query here to get records from DB. Then:
$query = $this->db->query();
$fields = $query->field_data(); 
foreach ($fields as $field)
{
      if ($field->type == 'date')
           // do conversion here
}

and do something similar for an UPDATE/INSERT callback. From what I can tell, there is no extra work on the database (because Codeigniter supplies the information in the $query result)

Thoughts?

edit 2: other idea: just do this for each model, and define which fields are date fields, and just apply the change at each callback… yeah… might do that…?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:49:11+00:00Added an answer on June 3, 2026 at 6:49 am

    Ok – answer to my own question – in case people search this and find it:

    I am using Jamie’s “MY_Model” – https://github.com/jamierumbelow/codeigniter-base-model

    Use callbacks on each of your models which have date fields:

        class Fake_model extends MY_Model
        {
            public $before_create = array ('date_convert_to_mysql');
            public $before_update = array ('date_convert_to_mysql');
            public $after_get = array ('date_convert_to_php');
    
            function __construct()
            {
                 parent::__construct();
            }
    
    
    protected function date_convert_to_mysql($post)
    {
        // Now convert the date field(s)
        $post['purchase_date'] = date("Y-m-d", strtotime($post['purchase_date']));
        return $post;        
    }
    
        protected function date_convert_to_php($result)
    {
        // Now convert the date field(s)
        $result['purchase_date'] = date("d-m-Y", strtotime($result['purchase_date']));
        return $result;        
    }
    }
    

    You’ll need to add foreach loops if selecting or adding multiple records. You could make it a generic callback which cycles each variable checking if it is a date – but it seems like alot of overhead. This way you just need to define the date field(s) once for each model

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario :- I have to call MYSQL stored procedure from PHP and do some
I am putting my php /mysql website up and this is my scenario The
I'm developing a php / mysql application that handles multiple simultaneous users. I'm thinking
How to implement pessimistic locking in a php/mysql web application? web-user opens a page
here is my scenario: I have a website with php, mySQL. I have a
I have an online application with PHP & MySQL. I need to provide some
I am interested in a scenario where webservers serving a PHP application is set
We have a php/mysql based application which has slightly different instances for a number
I am creating an application, built with PHP, MySQL and Doctrine v1.2, that deals
Scenario : We are in the process of converting an application from PHP 4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.