As of PHP manual:
Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
So you're better of with using DATETIME
type in your MySQL database and saving the timestamp after you convert it into valid format. For that I'd use DateTime
. Even better if you just use new DateTime
instead of time()
, as DateTime
takes time in constructor, which by default is now
:
public DateTime::__construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
If you were to use Doctrine 2, it would have been enough. Otherwise, you must format your DateTime object before inserting:
$dt = new DateTime();
$dt->format('Y-m-d H:i:s');
This will produce something like
2019-04-30 15:34:16
which is completely valid format for your DATETIME
type in your mysql database.
If you need to compare other DateTime objects, you can always use DateTime::diff
:
public DateTime::diff ( DateTimeInterface $datetime2 [, bool $absolute = FALSE ] ) : DateInterval
This will be enough for smart and representable operations with DateTimes and their comparison.
EDIT:
As you wrote
Ok, I converted the db field into a "DATETIME" field. But now I can't compare the result of the table field with a DateTime () object. Do you know how I can convert the table field to be comparable with a DateTime () object?
You need to create the DateTime
object from the data you have in your database. It is achieved by using createFromFormat
:
public static DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] ) : DateTime
So what you do is:
$dt = DateTime::createFromFormat('Y-m-d H:i:s', '2019-04-30 15:34:16');
and voila!