Keertan
Keertan

Reputation: 113

current timestamp generation in php and updating in mysql

i want to create a timestamp by which i can know which post is modified when and all. in mysql databse, i made a coloumn called lastmodified, with type as timestamp. now, when i am updating the data in db, i want to update the current timestamp in last modified. how to do so? also could anyone please tell me, if any function exits for comparing these timestamps.

$now = time();

$query = "update storydb set lastmodified = '$now' where story_id = '$story_id'";
mysqli_query($con, $query);

Upvotes: 0

Views: 1334

Answers (3)

Ambarish Yadav
Ambarish Yadav

Reputation: 412

To insert current unix timestamp in data base

$time = time();
$qry = 'update db_name' set column_name = $time where condition;
mysql_query($qry);

Upvotes: 0

Pupil
Pupil

Reputation: 23958

time() returns UNIX Timetamp in integer format e.g. 1223485636.

You want it in 2014-12-10 02:02:36

Use MySQL now() function instead of $now

$query = "update storydb set lastmodified = now() where story_id = '$story_id'";

now() is a MySQL function that returns current Time Stamp (including date).

Upvotes: 3

Kevin
Kevin

Reputation: 41885

No, its not unix timestamp that should be used in there, just a normal NOW() should suffice:

$query = "UPDATE storydb SET lastmodified = NOW() WHERE story_id = ?";
$update = $con->prepare($query);
$update->bind_param('s', $story_id);
$update->execute();

Upvotes: 2

Related Questions