2

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
  • Is this SQL or MySQL, you've tagged both? Commented Oct 17, 2011 at 12:48
  • @Stefto: is your table called enrties or is it a typo? Commented Oct 17, 2011 at 12:53
  • it was a typo, but i decided to keep it that way in the db itself so i could tell them appart XD Commented Oct 17, 2011 at 12:55

3 Answers 3

1

MySQL date format has to be YYYY-MM-DD

Turn it around so it looks like:

$_SESSION['Datum'] = $DateJaar.'-'.$DateMaand.'-'.$DateDag;
Sign up to request clarification or add additional context in comments.

1 Comment

already did that once but if it were that easy i wouldn't be posting it here would i? ;)
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.