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
Related
I am storing the time a player joins my server using a datetime datatype and inserting a CURRENT_TIMESTAMP. All this done in MySQL.
This will look something like this: 2014-04-14 02:15:03
However, on my website I want to display the time they join but on a more friendly way such as:
April 14, 2014 2:15CST
In a nutshell, how do I change "2014-04-14 02:15:03" into "April 14, 2014 2:15CST"?
Thanks!
With your mysql timestamp in var $datetime:
$formatted = date("F d, Y H:ie", $datetime);
Refer to date in the PHP manual :)
Try to use it like the following:
date('F d, Y H:i', strtotime($mysqlTimestamp));
For more details, check the PHP date function at: http://php.net/manual/en/function.date.php
You can do this:
SELECT DATE_FORMAT(NOW(),'%M %d %Y %h:%i %p') or SELECT DATE_FORMAT(CURRENT_DATE,'%M %d %Y %h:%i %p')
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
I have a date stored in MySQL as 2012-11-00 and I am trying to convert it so I can format it properly on a PHP page. When I use the following I always end up getting one month behind. So if the date in MySQL is 2012-11-00 then the result from PHP is Oct 2012.
date("M Y", strtotime($resVal['dateField']))
Any thoughts as to why it does this?
#1.
Why dont you have dates saved as 2012-11-01?
Because 2012-11-00 is "invalid", so for PHP that date is equal to 2012-10-31.
#2.
Use DateTime::createFromFormat which can handle your (strange) format with ease:
echo DateTime::createFromFormat('Y-m-|??', '2012-11-00')->format('M Y');
will return Nov 2012
#3.
Format date in MySQL with DATE_FORMAT function:
SELECT DATE_FORMAT(date_field, '%b %Y')
FROM table
Save the date as "2012-11-01", since 2012-11-00 is not valid.
2012-11-00 is not a valid date
2012-11-01 is a date...
So probably it is rounding to 2012-10-31
I have dates stored in a mysql table, they are set to store as CURRENT TIMESTAMP in each row and are stored as follows:
2010-05-29 01:17:35
but what i am trying to do is somehow use PHP to be able to seperate everything and create a date more like:
May 29 2010 1:17 AM
can anyone at least direct me in the right path that i should take. any help is greatly appreciated!
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
http://php.net/manual/en/function.date.php
You have two solutions :
Use strtotime() to parse the date to a timestamp, and date() to re-format it to a string
Or use the DateTime class
In PHP code, this would mean using :
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
Or :
$dt = new DateTime('2010-05-29 01:17:35');
echo $dt->format('M j Y g:i A');
strtotime + date is the solution you'll see used the most ; but it is not the best solution : with those, you'll work on UNIX Timestamps, which means a limited range of dates (from 1970 to 2038, if using 32 bits integers).
ON the other hand, using the DateTime class, there will be no limit to the range of dates you can work with.
If you have a DATETIME field in your table and you only want the date field, then:
SELECT DATE_FORMAT(timestamp,'%M %D, %Y') FROM Mytable;
or:
SELECT DATE_FORMAT(timestamp,'%Y-%m-%d') FROM Mytable;
where timestamp is your column name.
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']);