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;
Related
I have three data types in my database. Datetime, timestamp, and time. I get the time using the date function and tried to insert it into the database under all three columns, but all three columns rejected the A.M, P.M part of the date function. I don't understand why. I need the A.M, P.M part of the date function to be also inserted, so I can sort the data in my database more efficiently by time. Is there another column that can store the A.M, P.M part of the date, or is there a workaround this? Thanks.
$time = date('h:i:s A');
//insert $time into all three columns into the database
The workaround is to use the hours value as 24 hour clock. To represent '3:45:56 PM', insert to the database column a string value '15:45:56'.
Otherwise, you can use the STR_TO_DATE function to convert the string into a valid TIME value.
INSERT INTO mytable (mycol) VALUES (STR_TO_DATE('3:45:56 PM','%h:%i:%s %p'))
To retrieve a TIME value from a database column, formatted as 12 hour clock with AM/PM.
SELECT DATE_FORMAT(mytimecol,'%h:%i:%s %p') AS mytimestr FROM ...
References:
STR_TO_DATE https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
DATE_FORMAT https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Change the column data type as Varchar(20)
and insert $time = date('h:i:s A'); into database.
Get from DB:-
while fetching this column from database u can use
date('h:i:s A',strtotime($fetched_datetime_from_db)).
Enjoy!!
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 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've run into the following problem:
I had to migrate a database from MS SQL Server where some fields contained date values that weren't stored as such. For example there was a field "offers" with an "validfrom" and "validto" field. Sadly, they used text fields (varchar) for that type of input and im having a hard time now to filter it properly by date. An example as follows :
datefrom = "21.01.2012"
dateto = "21.05.2012"
Now im trying to sort out the old entries by date using
$curDate = date('d.m.Y'); // Outputs 19.03.2013
in my PHP PDO i use the following query
$query = "SELECT * from mytable where validtill >= '$curDate'"
which would output
$query = "SELECT * from mytable where validtill >= '19.03.2013'"
still i am getting old entries with date entries from 2011. I think i am missing something - maybe i cant compare strings as "date". I tried changing the field from vharchar to date but when i do, the whole imported data gets messed up.
Any advice?
Thanks!
In your question, you say you had to migrate from MS Sql Server -- what database did you migrate to? You need to convert the varchar field to a date to do a date comparison.
Assuming MySQL, then you could use str_to_date. Something like this should work:
select str_to_date(dtfield, '%d.%m.%Y')
from yourtable
where str_to_date(dtfield, '%d.%m.%Y') > str_to_date('1/20/2012', '%m/%d/%Y')
SQL Fiddle Demo
Assuming you're still in SQL Server, then you'd have to strip out each part of the date to put the date in a format SQL Server understands and then use CONVERT to change its datatype to a date:
SELECT CONVERT(datetime,RIGHT(dtfield,4)+SUBSTRING(dtfield,4,2)+LEFT(dtfield,2))
FROM yourtable
WHERE CONVERT(datetime,RIGHT(dtfield,4)+SUBSTRING(dtfield,4,2)+LEFT(dtfield,2)) > '1/20/2012'
More Fiddle
Right. That will do a string comparison and seems like you have the DAY first. Which means march 21, 2013 is greater than march 19, 2014... because the first thing it compares to sort is the day.
This text string comparison would work if you could rearrange the data to Y.m.d, that way 2013 > 2012 > 2011, and 2013-02-xx > 2012-02-xx...
My advice would be to make a new column in your database, and you can use a PHP script to convert and store the new correct values as dates.
Maybe that:
$query = "SELECT CAST(m.field as date) as field, m.* from mytable as m where m.field >='" . date('Y-m-d') . "';";
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