Welcome Guest, Not a member yet? Register   Sign In
Format date before save or show
#1

im italian , in my form the date are in the dd-mm-Y format , how can i format  in iso before save and update and  format from db before show on page?
Reply
#2

You want to format it when you display it in your form.

PHP.net date

You use the format. See the format options.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(01-28-2021, 12:21 PM)InsiteFX Wrote: You want to format it when you display it in your form.

PHP.net date

You use the format. See the format options.

but in the input form the date format is dd-mm-Y in db Y-mm-d
Reply
#4

(This post was last modified: 01-28-2021, 03:06 PM by InsiteFX.)

Take the date and convert it to a Timestamp then MySQLi will be able to use it.

You will also need to convert it back for the input if you use setVar.

You can also use the object date format.

Has better examples then date.

PHP.net - DateTime::format
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 01-28-2021, 11:03 PM by demyr.)

in the input? So, probably you are using a jquery ?? If yes, please check jqueryUI and the relevant part of it
Reply
#6

(This post was last modified: 01-30-2021, 07:00 AM by Codinglander.)

Hi Pip

I'm from germany and I had the same problem. My site supports two languages with different format of the date to input.

In my model (CI4) I made two changes:

First:
PHP Code:
protected $beforeInsert = ['getFormatedDate'];
protected 
$beforeUpdate = ['getFormatedDate']; 
This code will execute AFTER validation, but BEFORE inserting into database. 

And second (the callback code for the $beforeInsert/$beforeUpdate):
PHP Code:
protected function getFormatedDate(array $data)
    {
        //dd($data);
        $pattern "/^[0-3][0-9][.][0-1][0-9][.][2-9][0-9][0-9][0-9] [0-2][0-9][:][0-5][0-9]$/";
        $format "d.m.Y H:i";
        
        
if(preg_match($pattern,$data['data']['starts_at']) == 1){
            $data['data']['starts_at'] = \DateTime::createFromFormat('d.m.Y H:i'$data['data']['starts_at']);
            $data['data']['starts_at'] = $data['data']['starts_at']->format('Y-m-d H:i:s');
        } else {
            $data['data']['starts_at'] = \DateTime::createFromFormat('Y-m-d H:i'$data['data']['starts_at']);
            $data['data']['starts_at'] = $data['data']['starts_at']->format('Y-m-d H:i:s');
        }

        if(preg_match($pattern,$data['data']['ends_at']) == 1){
            $data['data']['ends_at'] = \DateTime::createFromFormat('d.m.Y H:i'$data['data']['ends_at']);
            $data['data']['ends_at'] = $data['data']['ends_at']->format('Y-m-d H:i:s');
        } else {
            $data['data']['ends_at'] = \DateTime::createFromFormat('Y-m-d H:i'$data['data']['ends_at']);
            $data['data']['ends_at'] = $data['data']['ends_at']->format('Y-m-d H:i:s');
        }
        //dd($data);
        return $data;
    

The third thing I made is to check the input data via validation IN THE LANGUAGE THE USER SET.
For this I created own rules for each language:
PHP Code:
public function format_date(string $str null): bool
    
{
        switch(session()->get('locale')){
            case "en":
                $pattern "/^[2-9][0-9][0-9][0-9][-][0-1][0-9][-][0-3][0-9] [0-2][0-9]:[0-5][0-9]$/"// English date YYYY-MM-DD HH:ii
                $format "Y-m-d H:i";
                break;
            case "de":
                $pattern "/^[0-3][0-9][.][0-1][0-9][.][2-9][0-9][0-9][0-9] [0-2][0-9][:][0-5][0-9]$/"// German date: DD.MM.YYYY HH:ii
                $format "d.m.Y H:i";
                break;
        }
        if(preg_match($pattern,$str) != 1){
            return false;
        
        return true;
    

I have in my database for each language ONE DateTimeFormat : Y-m-d H:i:s

I hope, I could help yout to solve your problem ;)

Greetinxx

Kighlander
Reply




Theme © iAndrew 2016 - Forum software by © MyBB