JamesB123
JamesB123

Reputation: 113

Getting blank date adding date to database (wordpress, php)

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

Upvotes: 0

Views: 1642

Answers (2)

George Yates
George Yates

Reputation: 1247

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.

Upvotes: 3

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

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); ?>" />

Upvotes: 1

Related Questions