Format a given date? [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Format a given date? [SOLVED]

Post 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.
Last edited by tomprogers on Thu May 11, 2006 12:29 pm, edited 1 time in total.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post 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"));
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think you want strtotime...

Code: Select all

<?php
echo date('m/d/Y', strtotime("2006-05-09"));
?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

If you are getting the date from a database don't forget to look at MySql date functions.
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Post 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-(
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Post by tomprogers »

Thanks guys. Those two things were exactly what I needed. :-)
Post Reply