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
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 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");
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 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!
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"