A Clockwork Orange
A Clockwork Orange

Reputation: 24843

PHP -Comparing Unix Timestamp To Now

I'm retrieving a unix timestamp from a DB and I want to check if this datetime has passed already.

I tried using an if statement to compare to time() but it always says the time has passed already.

What am I doing wrong?

EDIT: Just some more info..to determine am/pm I'm adding 12 to the hour if its PM before running it through mktime(). (Is this right?)

It's stored in the DB as int not as any datetime types.

Upvotes: 1

Views: 1650

Answers (3)

Matthew
Matthew

Reputation: 48304

If you are using mktime to create a UNIX timestamp, PHP is using the timezone settings to interpret what you mean by the given parameters. It's possible that you should be using gmmktime. It depends on how the timestamps in the database are being created; I cannot say for sure without seeing more code and having a more detailed explanation.

I generally prefer to simply store all dates as DATETIME types in the UTC (GMT) timezone. It tends to be less confusing.

Just some more info..to determine am/pm I'm adding 12 to the hour if its PM before running it through mktime(). (Is this right?)

  • 12 PM is hour 12.
  • 1 PM is hour 13.

So you don't always add 12. (i.e., 12 Noon is the exception).

Upvotes: 0

ashein
ashein

Reputation: 487

Make sure the timezones in the DB and PHP are the same, use NOW() function to fill the DB column with current timestamp (the column should be of datetime type), then you can get the timestamp using UNIX_TIMESTAMP() MySQL function which compares against PHP's time() just nice.

Alternatively, you can fill the DB column with something like

mysql_query("INSERT INTO your_table (your_date) VALUES (FROM_UNIXTIME(" . time() . "))")

That should work even with timezone discrepancies.

Upvotes: 0

spanky
spanky

Reputation: 1499

Your PHP time could be affected by PHP's timezone. Use date_default_timezone_get() to find out what time zone you're in.

Upvotes: 1

Related Questions