Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

format mysql timestamp using php

Tags:

php

mysql

i have column named postDate defined as timestamp. when i print it directly:

echo $result['postDate'];  

i do get that what is stored(eg. 2011-03-16 16:48:24) on the other hand when i print it through date function:

echo date('F/j/Y',$result['postDate']) 

i get December/31/1969

what am i doing wrong?

many thanks

like image 704
daniel.tosaba Avatar asked Mar 17 '11 13:03

daniel.tosaba


People also ask

How to format MySQL timestamp in PHP?

PHP date() format when inserting into datetime in MySQL. This problem describes the date format for inserting the date into MySQL database. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The date can be stored in this format only.

How to change timestamp format in MySQL?

Example: For converting datetime to format – dd:mm:yyyy, you can use the following query: SELECT DATE_FORMAT('2020-03-15 07:10:56.123', '%d:%m:%Y');


2 Answers

try this.

date('F/j/Y',strtotime($result['postDate'])); 

as timestamp is required, not formatted date as second parameter. or you can also try

SELECT UNIX_TIMESTAMP(postDate) as postDateInt from myTable 

instead of SELECT postDate from myTable and then have this in your code.

date('F/j/Y',$result['postDateInt']); 
like image 173
DhruvPathak Avatar answered Sep 23 '22 17:09

DhruvPathak


The PHP date function looks for an int time() as the 2nd param. Try using strtotime()

echo date('F/j/Y', strtotime($result['postDate']) ); 
like image 35
Aaron W. Avatar answered Sep 21 '22 17:09

Aaron W.