adding a timestamp in data entries

adding a timestamp in data entries

بواسطة - Reefo Relaxo
عدد الردود: 2
Hi, I'm not sure if this is the right forum to ask this. 

But I created a form and I need that form to have a timestamp on the database.
"When was the form created?"
And I need the Date and Time to be displayed also in human readable when I called the table.

I'm new at moodle programming and programming in general.

THANKS IN ADVANCE.
متوسط التقييمات: -
رداً على Reefo Relaxo

Re: adding a timestamp in data entries

بواسطة - Leon Stringer
صورة Core developers صورة Particularly helpful Moodlers

Many database tables in Moodle have a timecreated and timemodified column.

When creating a new record you can do:

$record->timecreated = time();
$record->timemodified = $record->timecreated;
$DB->insert_record('mytable', $record);

And when updating a record do:

$record->timemodified = time();
$DB->update_record('mytable', $record);

That way the record stores the date/time when it was created, and the date/time it was last modified.

In the database Moodle stores date/time as a Unix epoch value. You can use userdate() to show these values in the a human-readable format.

See the Time API page in the dev docs for more information.