2

I have used a datetime field in my database for storing dates, whats the proper way to insert say todays date into that field using PHP?

Cheers,

2
  • 1
    which database are you using? Commented Mar 6, 2011 at 9:35
  • Do you want to know for an arbitrary date? Because for today I would use MySQL's NOW() function (assuming you use MySQL, I'm pretty sure other DBMS' offer the same). Commented Mar 6, 2011 at 9:37

5 Answers 5

3

Don't use PHP at all, but the database's built-in function for that.

Assuming you're using mySQL, the function's name is NOW().

11.7. Date and Time functions in the mySQL manual

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

Comments

2

i think you can use the php date() function

Comments

1
INSERT INTO table (`date`)VALUES (NOW());

Comments

0

Use the PHP function time() instead and store the value as a normal integer - that gives you much more possibilities.

Comments

0

As everyone else has said, using the MySQL function NOW() would probably be the best bet for a DATETIME column. MySQL also has many date and time functions available.

Personally, I do what Ivarska suggested and use an INT data type with PHP's time() or MySQL's UNIX_TIMESTAMP(). It gives the date in the GMT standard and it will port over easily.

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.