Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql timestamps and php date()

Tags:

php

mysql

The mysql timestamp is in a standard format 2013-02-20 02:25:21, when I use date('H:i:s',$date) I get the same invalid output 18:33:33, how can I get the right output? hour:minute:seconds

like image 527
user2177393 Avatar asked Mar 20 '13 04:03

user2177393


People also ask

How can I get current date and time in MySQL and PHP?

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.

What does date () do in PHP?

Specifies the format of the outputted date string. The following characters can be used: d - The day of the month (from 01 to 31)

What is difference between timestamp and datetime in MySQL?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.


2 Answers

use this date('Y-m-d H:i:s',strtotime($date));

like image 129
Praveen kalal Avatar answered Oct 13 '22 21:10

Praveen kalal


Tip : try with MYSQL DATE_FORMAT function

SELECT DATE_FORMAT('2013-02-20 02:25:21', '%H:%i:%s');

if you want to do it only with PHP then use strtotime

 date('H:i:s',strtotime('2013-02-20 02:25:21'));
like image 30
diEcho Avatar answered Oct 13 '22 19:10

diEcho