0

Basically I'm trying to add a date to my database (just a date, not datetime)

here's what I have so far:

$wpdb->insert(
    'my_table',
     array('date-adding'=>date('Y-m-d', '1994-06-14')), 
     array('%F')
);

it simply inserts 0000-00-00 so its something simple that's wrong - probly the %F but can't figure out what.

Thanks

3
  • A missing inverted comma perhaps? Although quite why that doesn't just error out, I'm not sure. Commented Aug 27, 2013 at 16:00
  • 1
    is it a typo or are you missing a '? Commented Aug 27, 2013 at 16:00
  • 1
    RTFM: php.net/date date() does NOT use a string for its timestamp input. You're trying to format the number 1994, which is all that's going to be left of your date string after PHP tries to conver it to an integer, meaning you're going to be dealing with Jan 1st, 1970, approximately 12:33am. Commented Aug 27, 2013 at 16:01

2 Answers 2

3

Your PHP date() function is wrong, what are you trying to accomplish? The second parameter should be a UNIX timestamp, not a date string. Since you're just feeding it a static time string too, just use '1994-06-14', no reason to try and use the date() function.

If you want something dynamic using a string, try this:

date('Y-m-d', strtotime($dynamic_date_string))

EDIT AFTER DISCUSSION: The root problem turned out to be the third parameter formatting string formatting the DATE as a float. Since there was no reason to format the date, removing that optional parameter fixed the issue. Also, formatting the input as a string using %s would have worked.

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

15 Comments

Simply the user has an input - they enter their date of birth and it saves it to the database. So i'm simply trying to add any date I have as a string to my database but everything keeps coming out blank :S
@JamesB123 You could show us your input form then, to make it clearer as to "how" to answer.
Sorry I shortened everything for the question - right now my code is literally the above - i'm just passing a static string in ('1994-06-14') into my database table, all that's missing above is the php tags and global $wpdb;
@JamesB123 Also, in your script, play around with showing what $wpdb actually did. I would, after my $wpdb->insert run var_dump($wpdb->last_query); exit(); to verify that the query executed was what I wanted.
Ya, the third parameter is optional and is really only needed if you're sanitizing data, since you're forcing a format on your input, you don't need it. Also, look at the documentation, as those variables will help you get out of a lot of sticky situations.
|
1

Possible solutions:

Use strtotime() and date():

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

Source: https://stackoverflow.com/a/2487938/1415724


EDIT #2

Have a look at this:

<html>
<head>
<title>..</title>
<body>
<form action="work1.php" method="post">
Value1: <input type="text" name="date"><br>
<input type="Submit"/>
</form>
</body>
</head>
</html>

<?php
$username="root";
$password="abcdef";
$database="test_date";
$date=$_POST["date"];  // Your First Problem Post value
echo $date;
echo "i m here";
$date = date("Y/m/d", strtotime($date));   // 2nd Problem Date Format
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");

$query = "INSERT INTO date VALUES('','$date')";

mysql_query($query);

mysql_close();
?>

Source: http://www.daniweb.com/web-development/php/threads/300356/date-stored-in-mysql-as-0000-00-00

or:

$date = date("Y-m-d", strtotime($_POST['date']));
$result = mysqli_query($link,"INSERT INTO visit_exclude_dates (date) VALUES ('{$date}')");

if(!$result) {
    echo "Error: " . $link->error);
    die();
}

$result->close();
$link->close();

Source: https://stackoverflow.com/a/16991316/1415724


"EDIT"

Given that the data is coming from an input form field, here is an updated answer.

If you're trying to pass your data as a "string", then use double-quotes instead.

I.e.: $string="1994-06-14"; as compared to $string='1994-06-14';

are not treated the same in PHP.

Try using the following as an example:

<input type="text" name="date_input" value="<?php echo htmlspecialchars($date); ?>" />

10 Comments

Still coming out blank :S
@JamesB123 It's harder for me than others to work without seeing full source code, sorry.
@JamesB123 Check what your DB table/values is set to.
@JamesB123 Have a look at this answer on SO stackoverflow.com/a/12120457/1415724
thanks was having a look at that but still nothing, the only thing I can think is that the %F should be something else - in phpMyAdmin the format is literally set to date and comes out as 0000-00-00 if I enter an sql query into myAdmin everything works fine
|

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.