Hello everybody.
There is a row in my db table I have set it's type as "TIMESTAMP". How should I fill in?
Should get the current timestamp in php and send it to that row, correct?
So how can I do that?

Dani AI

Generated

Short answer: you usually don't need PHP to generate the TIMESTAMP — let the database do it unless you specifically need the application time. As hinted, MySQL can provide the current value server-side, and as mentioned you can make the column supply/update that value in the schema. For simple creation/modified timestamps the cleanest approach is to have the DB fill them automatically and omit the timestamp column from your INSERTs.

If you do need to generate the value in PHP (for example to use a specific timezone or to stamp something before sending it to another service), create a proper MySQL datetime string and bind it with PDO. Example:

date_default_timezone_set('UTC'); // pick a timezone you intend to use
$now = (new DateTime('now', new DateTimeZone('UTC')))->format('Y-m-d H:i:s');

$stmt = $pdo->prepare('INSERT INTO mytable (col1, created_at) VALUES (:col1, :created_at)');
$stmt->execute([':col1' => $value, ':created_at' => $now]);

Notes and quick troubleshooting:

  • If you want the server to set the value, put the timestamp function in the SQL (not as a quoted/bound string) or set the column default in the table — that ensures the DB executes it.
  • Timezone mismatches are the most common source of “wrong” timestamps: check PHP's timezone, MySQL server/session timezone, and decide to store UTC (recommended) and convert for display.
  • Pick TIMESTAMP vs DATETIME consciously: TIMESTAMP is timezone-aware in MySQL and has a limited range; DATETIME is timezone‑naive and has a much larger range.
    For your case, , the simplest fix is schema defaults so your PDO code can stay straightforward.

Recommended Answers

All 5 Replies

Well, I still haven't got what to do exactly. Where should I put CURRENT_TIMESTAMP() in the script? I'm using PDO.
Would you please help me more?

in your database (phpadmin for example) use default current_timestamp and in attribute on update current_timestamp

UPDATE mytable SET timestamp_field = CURRENT_TIMESTAMP()

I understood. Thanks friends.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.