0

I am having some problems getting my date into my SQL table. I do not use datetime, but date.

This is the code I use, and the problem is that my SQL server doesn't recognize $date_add as a date and just puts the default 0000-00-00 in the date section...

if (isset($_POST['postbutton'])){
                $articlepost = nl2br($_POST['article'])."<br>";

                date_default_timezone_set('Europe/Oslo');
                $datepic = date(YYYY-MM-DD);


                $pictureurls = $_SESSION['urlpost'];
                $thumbnail = 123;
                $title = $_POST['title'];
                $date_add = $datepic;
                $articlepostimg = $articlepost.$pictureurls;

                $insertpost = $db->prepare("INSERT INTO posts (title,post,date_add,thumbnail) VALUES (:title,:post,:date_add,:thumbnail)");
                $insertpost->execute(array(':title' => $title, ':post' => $articlepostimg, ':date_add' => $date_add, ':thumbnail' => $thumbnail));
                unset($_SESSION['urlpost']);

            }

Here is what I see in my database after I submit my form:

enter image description here

1
  • You could use the built-in SQL NOW() function using DATETIME as your column's setting, or a few other similar column types. Commented Mar 6, 2014 at 0:35

3 Answers 3

1

Try the following:

$datepic = date("Y-m-d");

Here are the docs for date()

As for the question added in your comments, after you retrieve your date you'll need to do something like the following, where $orig_date is assigned the date retrieved from the database. As for converting it to Norwegian, I think you'd have to look into setlocale(), which I think warrants a different question.

$formatted_date = date('j, M Y', strtotime($orig_date));

Sign up to request clarification or add additional context in comments.

6 Comments

You dont know how I can get the norwegian translation out when I fetch the date back?
@FriedBitz fo' sho. Not sure what you mean, what does is a Norwegian date look like? Post an example of how you'd like it to look upon fetching it.
I'd like to have: 6. Mars 2014 instead of 6. March 2014. So change the month name :)
However, does that change the month name aswell? From English -> Norwegian?
@FriedBitz I don't know about converting to Norwegian, perhaps post another question?
|
0

You need to use either double quote or quotes to make date() function work propely

$datepic = date('YYYY-MM-DD');

Comments

0

This is just to add to already given answers after seeing OP asked for a language conversion in Norwegian, and by no means is it meant to step on anyone's feet, but as a complimentary answer.

You can use the following month conversion code which are in French, but you can easily modify it in Norwegian.

Notice that "Mars" is spelled the same way.

(This taken from my own code library)

<?php
// enter date format 2011-01-31 (Y-m-d)
function date_in_french ($date){
$week_name = array("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
$month_name=array("","Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août",
"Septembre","Octobre","Novembre","Décembre");

$split = preg_split('/-/', $date);
$year = $split[0];
$month = round($split[1]);
$day = round($split[2]);

$week_day = date("w", mktime(12, 0, 0, $month, $day, $year));
return $date_fr = $week_name[$week_day] .' '. $day .' '. $month_name[$month] .' '. $year;
}
$currentDate=date('Y-m-d');
echo "Current Date: ";
echo date('D')." ".date('d')." ".date('M')." ".date('Y');
echo "<br>";
echo "Date in French => ".date_in_french($currentDate);
?>

Comments

Your Answer

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