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");
Related
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?
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');
I am doing upcoming event which takes data from MySQL of another script (in this case, another Joomla extension)
Thanks to Alex Mihai, I know how to show upcoming event:
SELECT * FROM EventTable
WHERE Date > CURDATE()
ORDER BY Date
LIMIT 1;
Now I need to show a date of that event in a specific way. I have date of event in a row in this format (numbers as example): 2012-12-30
Is it possible to select only the middle characters (month number) from this row and make something like this:
if 12 = December, if 01 = January and etc. (month names are just example)
For a clear image I am trying to make an upcoming event with this data:
3 first letters of the month in native language, day of the month, event title (event title already works)
use DATE_FORMAT
SELECT DATE_FORMAT(columnName, '%M %d, %Y') eventDate
FROM tableName
by the way, this outputs January 01, 2013
for more formats, click the link below
Format Date Fields Using MySQL DATE_FORMAT()
With PHP you can also do it:
$date = strtotime($row['Date']);
echo date('D j', $date);
More on date() and formatting options: http://php.net/manual/en/function.date.php
I've have a date field in my table that stores dates from a form in a Weekday, Day Month, Year string.
Example: Wednesday, 02, November 2010
the field is also a varchar.
can I loop through the entire table with a php script to convert the dates to a mysql date format or perform this with an SQL query?
I'm not sure that I can preform some certain statistical reports that involve picking out certain dates and date ranges in the format I have now. What are my options?
No need to get PHP involved. It can be done directly in MySQL:
ALTER TABLE yourtable ADD fixeddate date;
UPDATE yourtable SET fixeddate=STR_TO_DATE(bad_date_field, '%W, %d, %M %Y');
relevant docs here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
PHP has a built in class called DateTime. Here is a snippet of code that should get you started.
$date = DateTime::createFromFormat('l, d, F Y', 'Wednesday, 02, November 2010');
echo $date->format('Y-m-d');
You can find more info about this class at http://au.php.net/manual/en/book.datetime.php
I am creating a system which updates the user activity when something is done.
I have a variable $userhistory= 'User edited '.$info.' on July 14 2010 or (07-14-2010);
I want to know how can i get the date automatically. for sql query i am using NOW(), but in a variable like $userhistory how do i get the date and it want it only in either of these forms. not along with the time.
Also, I am updating the column userhistory in the database, which is a text field. Is this the correct way to do it? How can i save only 5 or 10 of the last few updates?
If you are using NOW() when you update or write an entry to the database, the column storing the date (userhistory?) should be of DATETIME type.
Then you'd you'd run your SQL as normal :
SELECT field1, field2, UNIX_TIMESTAMP(userhistory) FROM table;
Then in PHP, use date() on the database result to format it accordingly:
// July 14 2010
date('F j Y', $row['userhistory']);
// 07-14-2010
date('m-d-Y', $row['userhistory']); ,
You can do it without PHP as well.
SELECT
DATE_FORMAT(date_column, '%M %d %Y') AS 'Formatted',
DATE_FORMAT(date_column, '%m-%d-%Y') AS 'Formatted2'
FROM
table;