2

I am pulling the value EventDate from a table called events. At the moment is giving me a timestamp along with the date - 2012-05-29 00:00:00.

Is there anyway to trim this in PHP?

1
  • 5
    Why not do it in MySQL already? Instead of SELECT eventDate FROM events, do SELECT DATE(eventDate) FROM events.
    – Konerak
    May 29, 2012 at 12:09

3 Answers 3

12

Try using MySQL's build in functions such as DATE_FORMAT() or even DATE()

SELECT
  DATE_FORMAT(EventDate, '%Y-%m-%d') AS eventDate
FROM events;

 

SELECT
  DATE(EventDate) AS eventDate
FROM events;
8

If don't want to store time stamp in DB,then change field type from datetime to date.Then while retrieving you will get only date.

OR

$date = //store DB date in this variable;
date('Y-m-d',strtotime($date));
2

If you really need to do this in PHP, use the date() function.

$date = '2012-05-29 00:00:00';
echo date('Y-m-d', strtotime($date));  // 2012-05-29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.