• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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
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
in the input? So, probably you are using a jquery ?? If yes, please check jqueryUI and the relevant part of it
Reply

#6
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.