Page 1 of 1

Format a given date? [SOLVED]

Posted: Tue May 09, 2006 6:03 pm
by tomprogers
Is there any easy way to format a given date string? I see that the date() function can format a date, but it expects a timestamp, and I only have the string value pulled from a DB.

When I run date('m/d/Y', "2006-05-09"), it does not recogize the date and instead gives me the Unix epoch (I'm on Linux). This is, however, the way the vast majority of all the dates I'll be dealing with are formatting - they were originally human-friendly (e.g. 5/9/2006), were stuffed into a DB, and are now being pulled out and displayed - so I'm looking for the proper, elegant way to deal with this.

Posted: Tue May 09, 2006 6:59 pm
by litebearer
Try

Code: Select all

<?PHP
$ts = time("2006-05-09");

echo date('m/d/Y', $ts);

?>
or more simply

Code: Select all

<?PHP
echo date('m/d/Y', time("2006-05-09"));
?>

Posted: Tue May 09, 2006 7:09 pm
by RobertGonzalez
I think you want strtotime...

Code: Select all

<?php
echo date('m/d/Y', strtotime("2006-05-09"));
?>

Posted: Tue May 09, 2006 8:07 pm
by hawleyjr
If you are getting the date from a database don't forget to look at MySql date functions.

Posted: Tue May 09, 2006 8:36 pm
by tomprogers
hawleyjr wrote:If you are getting the date from a database don't forget to look at MySql date functions.
I'm using PostgreSQL, not MySQL. 8-(

Posted: Tue May 09, 2006 8:39 pm
by hawleyjr
tomprogers wrote:
hawleyjr wrote:If you are getting the date from a database don't forget to look at MySql date functions.
I'm using PostgreSQL, not MySQL. 8-(

PostgreSQL has all the functions you need also :)
to_char(current_timestamp, 'Day, DD HH12:MI:SS')
http://www.postgresql.org/docs/8.0/inte ... etime.html
http://www.postgresql.org/docs/8.0/inte ... tting.html

Posted: Thu May 11, 2006 12:30 pm
by tomprogers
Thanks guys. Those two things were exactly what I needed. :-)