Delete table older than 10 days - php

PROBLEM HAS BEEN SOLVED
ANSWER:
delete from `[table name]` where [row name] > AddTime( CurTime(), '14400 hour' )
This instantly deletes anything that is older than 10 days OLD.

first change string into DATETIME with help of STR_TO_DATE
It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME
SELECT STR_TO_DATE('Wed Nov 21 2012', '%a %b %d %Y' )
// return 2012-11-21
then use DATEDIFF
DATEDIFF(expr1,expr2)
DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other.
SELECT DATEDIFF(CURRENT_TIMESTAMP(),STR_TO_DATE('Wed Nov 21 2012','%a %b %d %Y'))
//return 8
so complete query will be
$sql="DELETE FROM `journal`
WHERE DATEDIFF(CURRENT_TIMESTAMP(),
STR_TO_DATE('journal_date','%a %b %d %Y')
) > 5";
Note : in STR_TO_DATE function
use %d if you store day of month part with leading Zero ( 01..31 )
OR use %e if you store day of month part without leading zero (1..31)

MySQL date/time functions all work on MySQL DATE and DATETIME values of the format:
2012-11-28
2012-11-28 22:16:00
If you are actually storing your dates in the format you're specifying, MySQL cannot parse that.
For information on MySQL's date/time functions, see: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

You Can Use cron jobs to run specified script

delete from journal where journal_date < date_sub(curdate(),interval 5 day)
it can use index on column journal_date
but "datediff(now(), journal_date) > 5 " can't use index on column journal_date

Apparently the order of the dates matters. According to the MySQL reference for datediff() your dates are backwards, so you are probably only getting negative numbers (hence never more than 5, so no deletions).

Use abs() mathematical function to ignore negative date diffrence which is causing the problem apparently.
$sql="delete from `journal` where
abs(datediff(now(), STR_TO_DATE('journal_date', '%a %b %d %Y'))) > 5"

Related

Dispay date formate in Apr 02 in php and mysql

$sql="SELECT *
FROM events
WHERE sdate > CURDATE()
ORDER BY STR_TO_DATE (sdate,'%d %F %Y')
LIMIT 1";
You will be glad to code using timestamps for this, let me give you an example:
SELECT UNIX_TIMESTAMP(CURDATE()) will return something like 1586063538.
That is a timestamp, which is easy to work with.
Then in your case, you will have to do a 2 step code
First when retrieving from the database you must use:
SELECT field1, field2, ..., fieldN, UNIX_TIMESTAMP(sdate) as epoch
FROM events
WHERE sdate > CURDATE()
ORDER BY STR_TO_DATE (sdate,'%d %F %Y')
LIMIT 1;
where field1, field2, ... are the other fields you want and epoch will contain the epoch for that date.
Remember naming the fields is always a good practice when coding.
Then using PHP date() function you can parse an epoch timestamp as Apr 02 using:
$timestamp = 1586063538;
$day_formatted = date("M d",$timestamp);
echo($day_formatted); //will be Apr 05 in this case
More information for the date() function in the php documentation here: https://www.php.net/manual/en/function.date.php
If you want to dig deeper on epoch unix timestamps you can look here:
https://en.wikipedia.org/wiki/Unix_time

PHP Date format day of week

Im my slq statement I have this:
DATE_FORMAT(end_date,'%jS %M %Y') AS endDate
I expect the date to come out like 20th February 2017 but instead I get jS February 2017.
The jS isnt working. What am I doing wrong?
You need to use it as %D, not %jS
DATE_FORMAT(end_date,'%D %M %Y') AS endDate
You should use %D instead of %jS !
As stated here : https://www.w3schools.com/sql/func_date_format.asp
You are using the mysql DATE_FORMAT function, so you have to use the patter %D
From here:
%D Day of month with English suffix (0th, 1st, 2nd, 3rd, �)

How to show a day and date in timestamp data (PHP MYSQL)

i have a field with timestamp datatype in my database, this is the format when i added some record
example: this is chapter field of chapter table in database book
id chapter title timepost
1 1 ..... 2013-10-30 23:33:14
i want that format change in my web site like, sunday,10 october 2013 - 23:33
how to do that? thanks
*UPDATE:*Thank you for all your answer guys, all of your answer have worked correctly
Use the mysql DATE_FORMAT function in your SELECT statement:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
For your request you would need:
DATE_FORMAT(timepost,'%W,%e %M %Y - %H:%i')
Almost every language also has a date formating function, php use Date (http://php.net/manual/en/function.date.php) but I believe this must be supplied a int.
You can do like this;
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W, %d %M %Y - %H:%i');
Out put:
'Sunday, 10 October 2009 - 22:23'
For more information you can see :http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Use PHP date() function:
http://php.net/manual/en/function.date.php
I strongly recommend doing it on the client side. This way you can easily apply custom date and time formatting based in the users locale.
Im my opinion, formatting should not be done on the SQL server side. Moving string formatting from the SQL server to the web server takes the load from the SQL server and gives it more power to run queries.
Use DATE_FORMAT function
select date_format(timepost,'%W, %M %e, %Y %h:%i %p') from table;
date('l,j F Y - H:i', $timepost);
*You could substitite the format characters
Use PHP's date() function:
$date = date('l, d F Y - H:i', $date_from_mysql);
Use templates from PHP's Manual: http://php.net/manual/ru/function.date.php

Format mysql timestamp to something meaningful

My database uses a TIMESTAMP column for each article which gets data written to it whenever an article is written to the database. This is done automatically by the database using CURRENT_TIMESTAMP as the default value for the column.
Unfortunately, the date appears as 2011-11-24 19:26:57 which is not ideal for - well - anything.
What I'd like to do is write it to my page in the same format as Thu 24 Nov 2011 19:26:57.
Any advice?
which is not ideal for - well - anything.
You are wrong. It is indeed and exclusively ideal for the sorting dates.
And for the formatting both PHP and mysql has strtotime()+date() and DATE_FORMAT() respectively.
Also note that you may wish to change TIMESTMP to DATETIME format, as the former can be easily altered by accident and thus spoil whole database.
you can use MySQL DATE_FORMAT function which formates the timestamp in a way you like see this link http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
You could use MySql builtin function to format date-time column
SELECT DATE_FORMAT(your_col, '%a %d %b %Y %k:%i:%s')
FROM your_table
take a look at : http://php.net/manual/en/function.date-parse.php This builds an array of a string representing a date, you can then format "your" date however you would like to.
There's also the mysql function DATE_FORMAT
e.g. SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
-> 'Sunday October 2009'
You can do this in either PHP or MySQL:
PHP code:
<php
$formattedDate = strftime('%a %e %b %Y %T',strtotime($mysqlDate));
?>
MySQL code:
SELECT DATE_FORMAT('%W %e %M &Y %T', date_col);
Info: http://www.php.net/manual/en/function.strftime.php , http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
You actually have a couple of options. One of them is to use MySQL directly.
An example to match you need would be:
SELECT field1, field2, DATE_FORMAT(dateField, '%a %e %b %Y %T') AS theDateFROM table;
Another option is to use the date() and strtotime() functions from PHP with the timestamp directly:
<?php echo date("D j M Y H:i:s", strtotime($datetime_from_database)); ?>
I hope this is good for you
1
<?php date("D M j G:i:s T Y"); ?>
2
<?php date("F j, Y, g:i a"); ?>
1
Sat Sep 9 4:13:18 CDT 2006
2
September 9, 2006, 4:13 am

SELECT 2010-10-24 09:02:46 post as 'Oct 24, 2010 at 9:02AM'

How can I convert my date field from mysql from 2010-10-24 09:02:46 to post on my site as 'Oct 24, 2010 at 9:02AM'
(SELECT TIME_FORMAT(`dPostTime`, '%b %e %l:%i %p')) as post_time
This won't post the date, just the time.
Thanks so much!
SELECT DATE_FORMAT(dPostTime, '%b %e, %l:%i%p') AS post_time
To replace %b %e with "Today" :
SELECT
CASE WHEN DAY(dPostTime) = DAY(NOW()) THEN
DATE_FORMAT(dPostTime, 'Today at %l:%i%p')
ELSE
DATE_FORMAT(dPostTime, '%b %e, %l:%i%p') END AS post_time
The description of the TIME_FORMAT function says:
This is used like the DATE_FORMAT() function, but the format string may contain format specifiers only for hours, minutes, seconds, and microseconds.
So use the DATE_FORMAT function if you want to format the date as well.
I'd suggest the better option is to select it from the database as a timestamp value, and then use PHP's date() function to output the text value to the site. Timestamps are much easier to work with than formatted dates. Even if all you want to do is output it straight to the web page, I would still prefer to leave the formatting to PHP rather than SQL.
SQL:
SELECT UNIX_TIMESTAMP(dPostTime) as post_time ...
PHP:
print date('M d Y h:iA', $data['post_time']);

Categories