php/Mysql query with inserting date fails

2019-02-25 15:36发布

I have been searching for quite a while now and I can't figure out why my query isn't working for dates.

I want it to be imported to my database as a Date, this is a choice from higher up so here I am.

$insert_query = "INSERT INTO enrties(
`datum` )
VALUES (
'".mysql_real_escape_string($_SESSION['Datum'])."')

a few pages before the above i use this piece of code to fill the $_SESSION

$DateDag = $_POST['Dag'];
$DateMaand = $_POST['Maand'];
$DateJaar = $_POST['Jaar'];
switch ($dateMaand)
{
case 'Januari': $DateMaand = '01'; break;
case 'Februari': $DateMaand = '02'; break;
case 'Maart': $DateMaand = '03'; break; 
case 'April': $DateMaand = '04'; break; 
case 'Mei': $DateMaand = '05'; break;   
case 'Juni': $DateMaand = '06'; break;  
case 'July': $DateMaand = '07'; break;  
case 'Augustus': $DateMaand = '08'; break;  
case 'September': $DateMaand = '09'; break; 
case 'Oktober': $DateMaand = '10'; break;   
case 'November': $DateMaand = '11'; break;  
case 'December': $DateMaand = '12'; break;      
}
$_SESSION['Datum'] = $DateDag. '-'. $DateMaand. '-'. $DateJaar;

I want to have the date imported as a SQL Date format (preferably DD-MM-YYYY format).

SOLVED, there was a mistake in the switch Apparantly, not really sure what but it works ;)

3条回答
太酷不给撩
2楼-- · 2019-02-25 16:07

after date is inserted you can format after query $new_format=date('h:i:s d/m/Y',strtotime($date_string));

查看更多
SAY GOODBYE
3楼-- · 2019-02-25 16:12

The MySQL date format is actually YYYY-MM-DD, so if you want to insert a date (guessing your datum field is Date or Datetime) you have to set up your string in that way.

If you want to insert your date in a different way you should use this:

INSERT INTO enrties (datum) VALUES (str_to_date('01/02/2000', '%d/%m/%Y')); 

but this will save always the date in YYYY-MM-DD way, since is the default for mysql.

查看更多
劳资没心,怎么记你
4楼-- · 2019-02-25 16:13

MySQL date format has to be YYYY-MM-DD

Turn it around so it looks like:

$_SESSION['Datum'] = $DateJaar.'-'.$DateMaand.'-'.$DateDag;
查看更多
登录 后发表回答