tags:

views:

346

answers:

2

Hey, I am using DATETIME for my column for my database. I am trying to find the best way to format this date held in the row using PHP. I tried using date() but that would just print the current date. My row looks something like:

$row['date_added']

Any ideas?

A: 

Edit: Disregard. I was thinking date() took any kind of date format, similar to strtotime(). Look to ceejayoz's answer for the actual way to do it. No need to repeat him.

Andrew Ryno
Won't work - date() expects a UNIX timestamp, but a MySQL DATETIME field will come out as 0000-00-00 00:00:00.
ceejayoz
My bad. I must have been thinking of strtotime(). I'll edit my post so he doesn't get confused.
Andrew Ryno
+2  A: 

A couple options.

  1. Use MySQL's DATE_FORMAT function to format it.
  2. Use MySQL's UNIX_TIMESTAMP function to get a timestamp PHP's date() function can play with.
  3. Run the MySQL DATETIME through PHP's strtotime() function to get a timestamp PHP's date() function can play with.

As an example of #3:

print date('<format string>', strtotime($row['date_added']));

Just follow the documentation on date() to construct a format string to your liking and swap it in.

ceejayoz
Would i have something like this: strtotime($row['date_started'])
Coughlin
That'll get you a timestamp to use in the second argument of date(), yes.
ceejayoz
Like so?strtotime($row['date_started'],date("m/d/y"))
Coughlin
Other way around. date('m/d/y', strtotime($row['date_started'])). See updated answer.
ceejayoz
Thank you for your help!Ryan
Coughlin