Change MySQL's datetime into a readable piece of text in PHP - php

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')

Related

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

Formatting time in php like mysql's date_format

How can we format time in php like we do using mysql's date_format?
In mySql we do date_format(now(),'%M %e, %Y') as time. Can php's current time be formatted to make it look like January 3, 2013?
Yes. Check out PHP date.
echo date('F j, Y', time());
The code is:
date("Y-m-d",strtotime(date()))
More information on the date() function and some information on the strtotime() function

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

Displaying Date from database PHP

right now I'm storing a date in the database, in the format date("Y-m-d") and im storing it in date column.
Now, I've retrieved it from the database but how can i display it like
October 31st 2010
Thanks!
Convert the date to a timestamp using strtotime and format it using date.
echo date('F jS Y', strtotime($databaseDate));
The preferred way going forward should be the use of the DateTime class though:
date_default_timezone_set('Asia/Tokyo');
$date = new DateTime($databaseDate);
echo $date->format('F jS Y');
Use date_format in your SQL query.
Example: date_format(somefield, '%d-%m-%Y, %h:%i %p')

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