1

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.

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

That is elegantly simple! Works like a charm. I see what was wrong now. Also I noted I had $updateDate = $phpDate = strtotime( $sqlDate ); while $updateDate didn't even exist. - Thank you for your answer. I will check as accepted in 5 minutes when it lets me!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.