Mysql compare date time - php

I have varchar field in database and format of that field is like '10 Feb 2016 08:26 PM'.
Now I want to fetch upcoming data, So how can i get that ?
$ctime=strtotime(date("Y-m-d H:i:s"));
if($type=='1'){
$books = $objData->getAll("select *
from bookings
where custID='".$custID."'
and fromTime>'".$ctime."'");
}
But I am not getting correct result, please help.

First of all, as mentioned in comments you should use proper types for dates. To answer your question, it is still possible to achieve, using STR_TO_DATE mysql function.
$objData->getAll("select * from bookings where custID=".$custID."'
AND unix_timestamp(STR_TO_DATE(fromTime, '%d %b %Y %h:%i %p')) > ".time());
Link: Convert VARCHAR timestamp to TIMESTAMP?

Related

Changing date from query with PHP

I have a query (written to be easier from a class)
$cms->my_query('SELECT * FROM location');
Which will return an array
Though I have a DATE type in the mySQL Table which it is formatted like so 2014-06-22
Is there a way I can format so it's like this Nov 04 2008 11:45 PM with using DATE_FORMAT(NOW(),"%b %d %Y %h:%i %p") now I believe DATE cannot use this properly so i'd have to use DATETIME but if that is the case it's fine but how do I select all and change date at the same time?
Example
$cms->my_query('SELECT * FROM location DATE_FORMAT(NOW(),"%b %d %Y %h:%i %p")');
I just don't want that ugly 2014-06-22 and I have very little knowledge of mySQL and I am learning as I try new things out. So if someone who is more skilled please explain the best scenario for me, I'd like to learn and I am willing!
The first argument of DATE_FORMAT() is the date you want to format. Putting NOW() in there means you will return the current date.
First, you'll need to change the date column to DATETIME, then use that column as the first argument to DATE_FORMAT. Try this:
SELECT *, DATE_FORMAT(mydate ,"%b %d %Y %h:%i %p") as date_added FROM location
Where mydate is the DATETIME column from the table.
See demo
The column need to be in type DATETIME. With date_time_column is a column in location table. Should be like this:
$cms->my_query('SELECT DATE_FORMAT(date_time_column,"%m-%d-%Y %r") FROM location');

MySQL Delete Row That Is Older Than The Current Date

I am trying to delete events in my database that have a start date older than the current day.
I've used the NOW statement and it deleted all of the content within my table.
The database is updated daily with events and I want to delete the events that have passed.
Here is a sample of my sql statement:
mysql_query("DELETE FROM Detroit WHERE Detroit.startDate < CURDATE()");
startDate is the name of the column in the db where all of the date information is stored.
The dates appear as Fri, 25 Apr 2014 19:00:00. When I use the CURDATE or NOW date options within my statement, the whole table is deleted. How do I delete the rows with the dates older than the current date?
I suspect that your startDate column is not a datetime field, but it's a varchar instead.
This query should work:
DELETE FROM Detroit
WHERE STR_TO_DATE(startDate, '%a, %e %b %Y') < CURDATE()
Or you could try to substitute %e with %d. However, it is always a better idea to use a DATETIME column and not a VARCHAR column to store date and times, so you should create a new column startDatedt and update your table this way:
UPDATE Detroit
SET startDatedt = STR_TO_DATE(startDate, '%a, %e %b %Y %H:%i:%S')
and then you could just use date and time functions to delete the rows that you need:
DELETE FROM Detroit WHERE startDatedt < CURDATE()
Please have a look here to see how to compose a date format string.
If you're working with the UNIX Timestamp(Seconds after the 1st january 1970, this format is always in UTC), you can use this code:
mysql_query("DELETE FROM Detroit WHERE Detroit.startDate < ".time());
Let me know if you're using another format and I make another code snippet.
Try this code and let me know its result please:
mysql_query("DELETE FROM Detroit WHERE DATEDIFF(CURDATE(),startDate) > 0");

How to format a date field in mysql?

I am having a problem in mysql query, I have date saved in database in this format :
Wed, 26 Oct 2011 01:25:35 EDT
I want to sort the rows by date but this date format is not letting me do it. I have tried Date_FORMAT AND STR_TO_DATE function but couldn't get it working, could you please help me solve this?
Assuming you've modified your tables so you've got the original date field ('olddate') and want to put the reformatted 'native' date into a new field ('newdate'), then:
UPDATE yourtable
SET newdate = STR_TO_DATE(olddate, '%a, %e %b %Y %H:%i:%s')
however, MySQL does not store timezone information in its date/time fields, so you'd have to convert the EDT stuff to whatever TZ is desired (UTC?).
Once converted to native format, you'd do your sorting with:
SELECT ...
...
ORDER BY YEAR(newdate)
This format is just plain text to MySQL and you should have another one as datetime or timestamp with the same data but something that MySQL could understand.
add a new column
use PHP to strtotime your actual row and replicate that value to the new column.
…
profit!

Returning rows on a certain date in MySQL

I've looked through past questions - but couldn't find a good match. How do I return all MySQL rows where the column timestamp is on day X, where day X is contained in a PHP variable the following format: "13 Apr 2011", and the timestamp is in database is in the following format: "2011-02-25 22:00:17"?
SELECT * WHERE timestamp (need to add here)
Thanks!
If the date string (in your PHP variable) has a consistent format, you can use the MySQL function str_to_date to convert it into a DATETIME suitable for comparison.
SELECT * FROM tablename WHERE DATE_FORMAT(timestamp,'%e %b %Y') = 'date coming from php'
(It's the opposite of andri's suggestion, it converts the timestamp into a string for comparision)
What you need is STR_TO_DATE
SELECT * WHERE DATE(`timestamp`) = STR_TO_DATE('$var','%M %d %Y')
More here

Trimming or converting a mysql date field with php

I am displaying the contents of a datetime field in mysql in a table with php, and want to show just the date. Is there a way to convert or do this, or must the actual string be editied or trimmed?
Using strtotime...
A simple way is like this:
$date="2009-01-27 14:30:22";
echo strftime("%d %m %Y", strtotime($date));
i.e. turn your mysql date into a time value with strtotime, then reformat it with strftime.
However this only works for dates which can be represented by a unix timestamp - so no dates before 1st Jan 1970 or after 2038
Using PEAR's Date class...
The PEAR Date classes for a more thorough approach which should cope better with historical dates:
$date=new void Date("2009-01-27 14:30:22");
echo $date->format("%d %m %Y");
...or use your DB
Alternatively, have your db format it for you with DATE_FORMAT
SELECT col1,col2,DATE_FORMAT(col2, '%W %M %Y') as col2readable FROM mytable;
You could do it with PHP, but if you don't need the additional baggage, you could consider doing it in your query and return only what you need. This example uses DATE_FORMAT():
"SELECT col1, col2, DATE_FORMAT(dateCol, '%d-%m-%Y') as 'newDate'
FROM tablename"

Categories