Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pgSQL : insert date format (leading zeros)?

I want to insert a date into my pg database, but I haven't got a clue what the correct format is and the pg help isn't really helping.

I have my date from form in d-m-yyyy format. So leading zeros are omitted.

How can I insert this correctly, is there a function to add leading zeros (either pg or php) ?

like image 426
Lucas Kauffman Avatar asked Feb 23 '23 13:02

Lucas Kauffman


2 Answers

Check to_date(text, text) function (Table 9-21 contains all supported patterns):

SELECT to_date('1-9-2011', 'DD-MM-YYYY');
  to_date   
------------
 2011-09-01
(1 row)

As you see leading zeros are properly added in output date.

like image 91
Grzegorz Szpetkowski Avatar answered Feb 26 '23 04:02

Grzegorz Szpetkowski


INSERT INTO TheTable (the_date) VALUES ('yyyy-mm-dd')

is the correct order in SQL

$psql_str = date('yyyy-mm-dd', date_parse_from_format('d-m-yyyy', $date_str));

converts your $date_str to the expcted format.

like image 36
marc Avatar answered Feb 26 '23 02:02

marc