3 Replies - 374 Views - Last Post: 13 January 2013 - 06:51 PM Rate Topic: -----

#1 hwoarang69  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

php now() function

Posted 13 January 2013 - 06:19 PM

i am storing datetime in database by using php function now().

the problem is that when i get the date from database i get '2012-2-3-1:23:10'.

and i just want to get (m/d/year) only
Is This A Good Question/Topic? 0
  • +

Replies To: php now() function

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 800
  • View blog
  • Posts: 1,688
  • Joined: 30-January 09

Re: php now() function

Posted 13 January 2013 - 06:28 PM

PHP doesn not have a now() function as far as I know, so I'll assume you're using the SQL function NOW(). When you retrieve the data from the database, it comes back as whatever data-type the data is, in this case a date-time combination. If you want to present this information differently in PHP, use the date() function, like this:
$formattedDate = date('n/j/Y', $dateValue);


Keep in mind that if the data-type in SQL is datetime and not int, then you may have to convert the data to unix timestamp using the SQL UNIX_TIMESTAMP() function, so that the date() function can work with the data.

This post has been edited by e_i_pi: 13 January 2013 - 06:30 PM

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: php now() function

Posted 13 January 2013 - 06:29 PM

You can incorporate

DATE_FORMAT(yourdatefield, '%c/%e/%Y') AS an_alias

In your SQL statement. Detailed here.

This will return the field as a string. If you need it to become a PHP date-object (so that you might compare it to other dates) then you would, instead, need to use a PHP date/time function such as strtotime date, after you had retrieved the value(s) from the database. Edited: I don't think strtotime on its own is necessarily suitable.

Added: I would take e_i_pi's advice ahead of mine though - he's more knowledgeable.

This post has been edited by andrewsw: 13 January 2013 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10778
  • View blog
  • Posts: 40,134
  • Joined: 27-December 08

Re: php now() function

Posted 13 January 2013 - 06:51 PM

It's more standardized to use a Unix timestamp, and it makes it easier to work with the dates cross-platform. If you need to format it, do it on the PHP end for the output.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1