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, �)
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I've a string like this: 2018-02-21 15:10:23.
I need to show a date like this '23 Febbraio 2018' and, I used this code to convert the string to a DateTime:
$post_date_to_DateTime = new DateTime($post_date_string);
$post_date = $post_date_to_DateTime->format('Y-m-d');
Now, I have to extract Month in format Text and day and year in number to compose the date (in Italian).
There's a fast method using some php date functions or I have to create an array with all Months Italian names and then create a specific string?
Thanks in advance!
Try This (Your Requirement):
$post_date = date('j F Y', strtotime('2018-02-21 15:10:23'));
For More Formatting Please check PHP - date
http://php.net/manual/en/function.date.php
setlocale(LC_TIME, 'it_IT');
echo (strftime("%e %B %Y", strtotime("02/28/2002")) );
/*OUTPUT*/
28 febbraio 2002
Read before this https://stackoverflow.com/a/1114547/5803974
use strftime — Format a local time/date according to locale settings
echo strftime('%e %B %Y', strtotime('2018-02-21 15:10:23'));
%d Two-digit day of the month (with leading zeros) 01 to 31
%e Day of the month, with a space preceding single digits. 1 to 31
%B Full month name, based on the locale January through December
%Y Four digit representation for the year Example: 2038
For more info Click Here
according to https://secure.php.net/manual/en/function.date.php
you can get the english month name with:
$post_date_to_DateTime->format('F');
to get italian names you will have to define them by your self, like you already guessed.
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
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"
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
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']);