Joey Thomas
Joey Thomas

Reputation: 113

PHP Converting SQL date to different format showing as a string of numbers

I have a query which lists out rows from a database. I need to convert the standard SQL date format from Y-m-d H:i:s (2001-03-10 17:16:18) to F j, Y, g:i a (March 10, 2001, 5:16 pm) when echoing out the date.

To convert the date format I have

$sqlDate = $row->updateDate;
$updateDate = $phpDate = strtotime( $sqlDate );
$sqlDate = date( 'F j, Y, g:i a', $phpDate );

then echo out

echo $phpDate;

When $phpDate is echoed out it shows as a string of 10 numbers like this: 1454241452

I am not sure what those numbers are... seconds?

What am I doing wrong within the conversion of strtotime ? Or would my problem lie elswere?

Thank you for your time.

Upvotes: 1

Views: 694

Answers (2)

Vadivel S
Vadivel S

Reputation: 660

Your $phpDate variable value store strtotime but not correct.

You change your code some modify like this

$sqlDate = $row->updateDate;
$updateDate = $phpDate = strtotime( $sqlDate );
$sqlDate = date( 'F j, Y, g:i a', $phpDate );
$phpDate= date( 'F j, Y, g:i a', $phpDate );
echo $phpDate;

This code working fine

this code useful for You :)

Upvotes: 0

peixotorms
peixotorms

Reputation: 1283

You are echoing the wrong date var, try it like this:

$sqlDate = $row->updateDate;  # get date from database 
$utime = strtotime($sqlDate); # this is unix time (check php.net/date)
$newdate = date( 'F j, Y, g:i a', $utime); # convert unix time to desired format
echo $newdate;

or in one line:

$newdate = date( 'F j, Y, g:i a', strtotime($row->updateDate));
echo $newdate;

Upvotes: 3

Related Questions