I am having some trouble getting a date field out of my SQLServer using PHP and have not been able to Google a solution. I know that it involves the fact that the field is a date data type in my table, but the only results I get is a 500-Internal Server Error.
Here is what I have:
$query = "SELECT EntryDate FROM Table1 WHERE UserID = 1";
$result = sqlsrv_query($link, $query);
while($row = sqlsrv_fetch_array($result))
{
echo $row['EntryDate'];
}
This works with every other column in my table except the date column. Any idea why?
BTW, the date in my table is formatted as yyyy-mm-dd.
$row['entryDate']
seems to be an object of type DateTime which you can't print out directly. Try to convert it into a string in order to echo it.
echo $row['entryDate']->format('Y-m-d H:i:s');
or
echo date_format($row['entryDate'], 'Y-m-d H:i:s');
PHP: DateTime::format
Related
my query(select invoice_date from pr_tax_intra where invoice_date between '03-05-2018' and '05-05-2018')
invoice_date data type varchar->(invoice_date varchar(55) )
output images:
enter image description here
enter image description here This images select only 5 month data but show in 4&5 month data also ...
i am using mysql database
i have stored date as varchar(255) data type and Date format is dd/MM/yyyy now i have to convert it into date data type can u help me in query ?
i have 780 records and each recod contain the Date in format of dd/MM/YYYY but due to varchar data type i can not perform calculation on Date so i have to convert into date without data lose.
please help me
You can use myqsl STR_TO_DATE() function
SELECT STR_TO_DATE("10 August 2017", "%d %M %Y")
//For Testing
SELECT DATE_FORMAT(STR_TO_DATE('21/05/2018', '%Y/%m/%d')
,'%d/%m/%Y')
In your case use below query
SELECT DATE_FORMAT(STR_TO_DATE(YourVarcharDateColumn, '%Y/%m/%d')
,'%d/%m/%Y') from YourTable
You can do that like this,
get all your record and then apply a while loop
$ListContent = mysqli_query($conn, "SELECT `date_filed` FROM table_name") or die(mysqli_error($conn)." Error in query"); // $conn is connection object
while($arr = mysqli_fetch_array($ListContent)){
$currentDate = $arr['date_filed'];
$currentRowID = $arr['id']; // Unique id column name
$newDate = date("Y-m-d", strtotime($currentDate));
$sqlUpdate = "Update table_name SET `date_filed` ='".$newDate."' WHERE id=".$currentRowID;
mysqli_query($conn, $sqlUpdate) or die(mysqli_error($conn)." Error in update query");
}
This will make your all record in "yyyy-mm-dd" format and then change your field into date type
SQL Server (MSSQL)
SELECT FORMAT(YourDateColumn,'dd-MM-yyyy') from yourTable
date format: init(11)
date column value: 1421382119
I have managed to get the dates from the table.
$sqllast = $Db1->query("SELECT * FROM table");
while($row = $Db1->fetch_array($sqllast))
{
$date1 = date('Y-m-d', $row['create']);
echo "$date1";
}
Question:
I want to get values from this table using the dates interval. I am not getting idea which format shall I enter the dates.
I have tried y-m-d, d-m-y , but it did not work
I tried this query
SELECT * FROM table WHERE create between '2015-01-1' and '2015-01-30'
Since the column type is int, you have to convert your string dates to ints as well.
Try this:
SELECT * FROM `table` WHERE `create` BETWEEN UNIX_TIMESTAMP('2015-01-01') AND UNIX_TIMESTAMP('2015-01-30')
Since you are storing a unix_timestamp as your value in column create, you will want to use MySQLs UNIX_TIMESTAMP() to covert your dates to a unix_timestamp
SELECT * FROM table WHERE create between UNIX_TIMESTAMP('2015-01-1') and UNIX_TIMESTAMP('2015-01-30')`?
Well i built my website to write dates to database column in the following format:
2014/04/01
Now i've come to realize this is not helpful when i want to sort by date or manipulate date so i decided to change all date to to timestamps.
i applied the following fix :
$sql = mysql_query("SELECT * FROM TABLE")or(die(mysql_Error()));
while($info = mysql_fetch_array($sql)){
$newdate = strtotime($info['date']);
mysql_query("UPDATE TABLE SET date = '$newdate' WHERE id = $info[id]")or(die(mysql_error()));
}
the problem is all the date columns are empty now, What am i doing wrong ?
There's what looks like a syntax error on this line:
mysql_query("UPDATE TABLE SET date = '$newdate' WHERE id = $info[id]")
or(die(mysql_error()));
Try changing it to:
mysql_query("UPDATE TABLE SET date = '$newdate' WHERE id = {$info['id']}")
or (die(mysql_error()));
The reason is that when interpolating array indices into a string, you must surround them with {} or PHP will just try to convert $info to a string and insert [id] after it, which I'm guessing you didn't intend.
I would also suggest checking the return value for strtotime. If it can't parse a date it returns false which I'm guessing you don't want inserted back into your database.
I need to match the result format of 2 code:
I need to get the output/format of this:
$event_day = $year.'-'.$month.'-'.$list_day; // $event_day
match this:
DATE_FORMAT(date,'%Y-%m-%d')
Full code:
$query = "
SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date
FROM table
WHERE user_id = '$session_user_id'
AND date BETWEEN '$year-$month-1' AND '" . date("Y-m-t", strtotime("$year-$month-1")) . "'
AND active = 1";
My problem is that $event_day is only displaying events for: October, November and December.
I had a similar problem with this code below:
$query = "
SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date
FROM table
WHERE user_id = '$session_user_id'
AND date LIKE '$year-$month%'
AND active = 1";
and it was fixed with this code:
$query = "
SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date
FROM table
WHERE user_id = '$session_user_id'
AND date BETWEEN '$year-$month-1' AND '" . date("Y-m-t", strtotime("$year-$month-1")) . "'
AND active = 1";
Anyone know how I could sort this out?
If you are simply trying to match dates in a SQL query, which is what you look like you're after, you can just pass the date as a string with a # on each end. This will tell SQL to parse the string as a date and the format you give it in is not really important. The exception to this is it can get confused between US/EU date formats like dd/mm/yyyy vs mm/dd/yyyy.
$query = "SELECT title, DATE_FORMAT(myDate,'%Y-%m-%d') AS formattedDate
FROM table
WHERE user_id = '$session_user_id'
AND (myDate BETWEEN #$year-$month-1# AND #$year-$month-1#)
AND active = 1";
This format should work but your query is select records that fall between the same date so you'll only get records that are actually on that date. You might as well just use WHERE date = #$year-$month-1#
edit: this assumes your date field datatype is correctly set up in the database as a date/time.
edit2: "date" is often a reserved word in databases and shouldn't really be used for field names and variables. See edited code above.
In addition I would suggest using php to format the date using the date() function rather than format it in the query. This allows more flexibility to actually use the date in your script.
I have a mysql database of entries
with dates. So what I want is to show
all the dates in my database and then
under each date, I want to show all
the entries in the database entered on
the specefic date. I am thinking of
two loops but I don't know how to
write the condition to display all the
dates in my database before I loop out
the entries under that date.
<?php
$sql = 'select start_date, name from events order by start_date';
$res = mysql_query($sql) or die(mysql_error());
$prev_date = null;
while ($row = mysql_fetch_assoc($res)) { if ($row['start_date'] != $prev_date) {
echo "<h1>{$row['start_date']}</h1>"\n;
$prev_date = $row['start_Date']; }
echo "<p>{$row['name']}</p>"; }
?>
In a previous question (Looping out mysql data), I resulted in using this code. It pulls the date and time from MYSQL, and I used NOW() to store both date and time. How can I make it ignore the time so I can achieve what I want?
as David Andres mentions in the comment, DATE() extracts the date part of a date or datetime expression. so you can do the following:
<?php
$sql = 'SELECT DATE(start_date) AS start_date_date, name FROM events ORDER BY start_date';
$res = mysql_query($sql) or die(mysql_error());
$prev_date = null;
while ($row = mysql_fetch_assoc($res)) {
if ($row['start_date_date'] != $prev_date) {
echo "<h1>$row[start_date_date]</h1>\n";
$prev_date = $row['start_date_date'];
}
echo "<p>$row[name]</p>";
}
Use CURDATE() instead of NOW().
Try it with a condition like this:
SELECT * FROM `events` WHERE DATE(`start_date`) = '2009-09-09';
This'll get you all events from the database for Sep 9th 2009. I think that's what you're asking for, is it?
Untested code that I will probably need someone to correct, but here goes:
SQL to retrieve all the dates that exist in the table:
$sql_get_dates = 'select start_date from events order by start_date distinct';
And, assuming start_date is a DATETIME type, the SQL to get all events on a given date:
$sql_get_events = 'select * from events where date(start_date) = "2009-08-09"';
Instead of just selecting the date, you could use some of the MySQL time functions to truncate the date.
$sql = "select date_format(start_date, '%Y-%m-%d') as fstart_date, name from events order by start_date";
Of course, you'll have to change start_date to fstart_date within the PHP code.
Check out the Mysql reference page for DATE_FORMAT().