Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a timestamp is set in MySQL and PHP?

When a timestamp column is defined as DEFAULT 0, if it's not specified in INSERT or UPDATE, '0000-00-00 00:00:00' becomes the default value.

What's the proper way to check if such a timestamp is set thus not the default value?

SELECT * FROM table WHERE timestamp <> 0

It does seem to work but I'm not sure if it's the proper way and work 100% cases?

And when such value gets selected in PHP, how to check if it's set or not?

Thus far, I came up with something like this:

if ($row['timestamp'] != '0000-00-00 00:00:00') {
}

It does work but it seems tacky to me. I'm not so sure if its the proper way neither. What should I do instead?

like image 708
datasn.io Avatar asked Sep 17 '25 08:09

datasn.io


1 Answers

In Php side you can use this

 if ($row['timestamp'] > 0) { } 

and

in mysql

SELECT * FROM table WHERE timestamp > 0
like image 131
Anish Avatar answered Sep 19 '25 03:09

Anish