Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store date and time with MySQL PHP + AJAX

Tags:

ajax

php

mysql

I'm making a web based project management application using MySQL and PHP that uses JS (Jquery) in the front end. The user has to input a date and optionally time as well.

However I'm not sure how I should go about inserting and storing the date and converting it back to human readable form in the application.

Thanks in advance, RayQuang

like image 865
Rayhaan Jaufeerally Avatar asked Feb 26 '23 11:02

Rayhaan Jaufeerally


2 Answers

Always use the standard Date/Time types for the respective situation.

In MySQL, use one of the appropriate Date and Time Type. Don't just blindly use one type. If you're storing a date, don't use a timestamp. If you're storing a timestamp, don't use a date. Use the proper type and be done.

In PHP, you can use an integer (parse from mysql's type with strtotime().

Talking with JS, I'd suggest using RFC 2822 date format, since it's standard. That way, you're communicating externally using a standard date/time format (which is non-ambiguous).

like image 61
ircmaxell Avatar answered Mar 06 '23 23:03

ircmaxell


Store as timestamp.timestamp contain both date and time. and it will be best way to store date and time .

like image 45
XMen Avatar answered Mar 06 '23 23:03

XMen