How to insert a date in mysql via php? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i have date in a variable as
$d = 2014-MAR-20;
How to insert this $d value to a field having type 'Date' in mysql using php?.

$d=date('Y-m-d',strtotime($d));
Then insert it to your table with your date column.
mysqli_query($YourConnection,"INSERT INTO yourTable (date) VALUES ('$d')");

If you are using PDO
$d = "2014-MAR-20";
$date =date('Y-m-d',strtotime($d)); // convert to Mysql format
Insert date value using PDO prepared statement
$stmt = $db->prepare("INSERT INTO table(`date`) VALUES(?)");
$stmt->execute(array($date));

Related

Can't insert datetime to MySql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this weird thing when i get the date time: $date = date("Y-m-d H:i:s");
and try to insert it to a database, it will insert null. The column i insert it in is a datetime. It does not matter to what datatype i change the column it will still insert null.
I hope someone can help me!
Because the column is a datetime, in your query instead of
$sql = ".... datefield = '$date' ....";
do
$sql = ".... datefield = TIMESTAMP('$date') ....";
Obviously the .... represents whatever your sql is at that point.

PHP MYSQL: Match Date in MYSQL Query to PHP variable that holds a date [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to match a date from my database to a PHP variable that contains date data that was converted to a string. However my query is not returning any results.
The database field that contains the date data is 'datetime'.
PHP Code:
$todays_date = strtotime("today");
$converted_todays_date = date("m/d/Y", $todays_date);
$dates_sql = "SELECT UNIX_TIMESTAMP(datetime) AS tstamp FROM employee_datetable WHERE STR_TO_DATE(datetime, '%m/%d/%Y') = '$converted_todays_date'";
$result = $usermysqli->query($dates_sql);
// Additional Code that I forgot to add before
while($row = $result->fetch_array()) {
$FormattedPhpDate = date('M d, Y', $row['tstamp']);
echo "<th><div id=day" . $FormattedPhpDate . "</div></th>";
}
Right now there is no date being outputted. I actually put in the wrong part of the code before.
Since you are using DateTime Field you can simply send date in query as date(Y-m-d H:i:s); from PHP to match it exactly. If you only want to compare only date part then use Date() mySql function to perform the comparison.

How to do less than, more than with wpdb (wordpress database)? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How do I do the less than AND greater than $date in wpdb?
$date = ('Y-m-d');
$wpdb->get_results($wpdb->prepare('SELECT * FROM product_history WHERE enddate = "%d";', array($date)));
I wan't to get history where enddate is today or in present time, BUT also more than 0000-00-00.
Try this one
$wpdb->get_results($wpdb->prepare('SELECT * FROM product_history WHERE enddate >= CURDATE()'));
It will work definitely.
Incase of Date with Time, you have to select field DATETIME in MySQL and in that case you should run query something like
$wpdb->get_results($wpdb->prepare('SELECT * FROM product_history WHERE enddate >= NOW()'));
Using this query, You can filter data from current time.

Sorting month name in mysql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I stored the month name in database as string which looks like
Apr-2013
May-2013
...
How could i sort the table month wise ?
Any help is appreciated.
You will have to format the date in order to sort it:
select aDate from t
order by str_to_date(aDate,'%b-%Y')
This is very inefficient, though. I'd recommend you to update that field and make it a date field or at least two ints: one for the month and one for the year. Then, if you need to get the name of the month you could use the monthname(date) function.
SELECT
*
FROM
dates
ORDER BY
STR_TO_DATE(date, '%b-%Y')
SQL Fiddle

Select all columns but unix_timestamp createdate [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Using PHP/MySQL
I need to select all the columns in the table, but convert the column 'createdate', which is saved as a current_timestamp to a unix_timestamp within the query.
You can use MySQL's UNIX_TIMESTAMP() function:
SELECT *, UNIX_TIMESTAMP(createdate) AS unix_createdate FROM my_table
This can be achieved with the UNIX_TIMESTAMP() function built in MySQL.
mysql> SELECT UNIX_TIMESTAMP();
-> 1196440210
mysql> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
-> 1196440219
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp

Categories