Pergunta

I have this in my html for a result

<?php echo $row['end_date']?>

that results in 2014-01-31 22:00:00

I want it to echo this format

01/31/14 10:00pm OR January 31, 2014 10pm if possible

Any help will be much appreciated.

Foi útil?

Solução

The DateTime class is incredible for this.

You just need to create a DateTime object with the string that MySQL returns, and it will parse it automatically. Then, just call the DateTime::format method to format it the way you want.

<?php
$date = new DateTime($row['end_date']); // e.g. 2014-01-31 22:00:00
echo $date->format(DateTime::RFC2822); // result: Fri, 31 Jan 2014 22:00:00 +0100
echo $date->format('F j, Y g:ia'); // result: January 31, 2014 10:00pm
?>

For formatting the date, see the manual for the date() function.

Outras dicas

There are many ways to do this.

http://in2.php.net/strtotime

Many other options are available to format the date the way you want.

Thanks Amit

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top