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
php now() function
Page 1 of 13 Replies - 118 Views - Last Post: 13 January 2013 - 06:51 PM
Replies To: php now() function
#2
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:
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.
$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
#3
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 asstrtotime 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.
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
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
#4
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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|