0

I have a javascript datetimepicker that I downloaded online which I use to select a start time and end time of a booking.

However, when I select a date and time it is displayed in this format in the input box - 03/12/2012 04:00 pm, as a string I believe.

What I've done so far is use the strtotime function which is successfully converting the times into unix timestamps so they can be compared.

However, for storing the start and end times in my mysql database and displaying them to the user I need the date and time to be displayed in a more user friendly and readable datetime format such as 03/12/2012 04:00 or 03/12/2012 04:00:00.

Does anybody know if this is possible and if so how to do it?

Thank you!

2 Answers 2

1

You can use this workflow.

  1. Convert 03/12/2012 04:00 to timestamp by strtotime()
  2. Convert timestamp to MySQL format by date("Y-m-d H:i:s", $timestamp); or using DateTime

    $dt = new DateTime();
    $dt->setTimeStamp($timestamp);
    $mysql_date = $dt->format("Y-m-d H:i:s");
    
  3. When you retrieve the date from mysql parse it again with strtotime()

  4. Echo it back to HTML/JS as DateTime::format("d/m/Y h:i:s a");

    $dt = new DateTime($mysql_date);
    $user_friendly_date = $dt->format("d/m/Y h:i:s a");
    

Links of interest

Sign up to request clarification or add additional context in comments.

1 Comment

If you believe its helpful, why not up-vote so that it can stay on top
0

The MySQL database also supports storing datetime formats, e.g. 2012-03-11 23:44:15. If you store them as datetime they can also be easily compared against other dates. How about that?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.