When I echo['time']
from the Database i get for example
2012-07-21 17:00:00
my question is how should I show only the hours and minutes for every timestamp I get? (17:00)
In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.
The CURRENT_TIMESTAMP function in the MySQL database returns the current date and time (i.e. the time for the machine running that instance of MySQL). It is given as a value in the 'YYYY-MM-DD hh:mm:ss' format.
To get a day of week from a timestamp, use the DAYOFWEEK() function: -- returns 1-7 (integer), where 1 is Sunday and 7 is Saturday SELECT dayofweek('2018-12-12'); -- returns the string day name like Monday, Tuesday, etc SELECT dayname(now()); To convert a timestamp to a unix timestamp (integer seconds):
MySQL NOW() Function The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.
You can select it as:
SELECT TIME_FORMAT(`dateColumn`, '%H:%i') FROM ...
or use strtotime
as mentioned in other answers.
If you did want the full time with seconds, you could also use:
SELECT TIME(`dateColumn`) FROM ...
See http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
You should parse it properly and echo what you want with the DateTime class:
$time = '2012-07-21 17:00:00';
$date = DateTime::createFromFormat( 'Y-m-d H:i:s', $time, new DateTimeZone( 'America/New_York'));
echo $date->format( 'H:i'); // Will print 17:00
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With