0

i have this shopping cart in which it sends the orders to mysql database along with the date on when it was ordered: the code for the date is as follows:

<?
    include("includes/db.php");
    include("includes/functions.php");  

    $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=$_SESSION['cart'][$i]['qty'];
            $price=get_price($pid);
            $date=date('Y/m/d');
            $user=$_SESSION['username'];
            $pname=get_product_name($pid);
            mysql_query("insert into `order` values ('','$pname','$q','$price','$date','$user')")

                or die(mysql_error());

it outputs the date as 2011/11,14. How do i get it to output it like the following: 14/11/2011??

2
  • That code is extremely prone to SQL injections... Commented Nov 14, 2011 at 0:08
  • im not focusing on the security aspects yet, im trying to get the queries to work 1st Commented Nov 14, 2011 at 0:09

2 Answers 2

1

The issue is with the $date = date... line. I'm not going to give you the direct answer to your question, but have a look at the documentation for date() for starters. Also, you should really be using a DATE database field rather than a string to store that

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

Comments

0

Change this line :

date('Y/m/d');

for

date('d/m/Y');

4 Comments

if i change it to date('d/m/Y'); it returns with : 0000-00-00
In that case, it's because your date field is of data type DATE and expects inserted data to be in the MySQL date format, which is YYYY-MM-DD
yes it was DATE as a data type of which i have now changed it to an integer....it now outputs the date, not the month or year. my php code looks like this now: $date=date('d/m/Y');
No, it wanted to be the DATE data type. By changing it to integer, it'll just store the first number, or try and do some strange maths, or something just plain undefined. You need to insert it as Y/m/d as that is what MySQL expects (and it's stored as YYYY-mm-dd hh:mm:ss internally).

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.