Saulius Antanavicius
Saulius Antanavicius

Reputation: 1381

PHP unix time stamp in the past

I always struggled with this, making a unix time stamp in the past, I need to find all records in mysql that are older than 30 days, but can't seem to think of a way to do this without writing a lot of code, there must be a short way to do it?

Any help would be appreciated

Thanks

Upvotes: 0

Views: 1069

Answers (5)

GodFather
GodFather

Reputation: 3146

if you have a datafield so you can use something like mysql DATE_SUB to resolve:

SELECT * FROM mytable WHERE created_at < DATE_SUB(CURDATE(),INTERVAL 30 DAY);

Upvotes: 0

newenglander
newenglander

Reputation: 2049

I would use the mysql DATEDIFF function

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff

with NOW() as one of the arguments. The number of days between the dates is returned.

Upvotes: 0

Josh
Josh

Reputation: 8191

Using PHP/MySQL:

$thirtydaysago = time() - (60 * 60 * 24 * 30);

$query = "SELECT * FROM records WHERE time < '$thirtydaysago'";

Upvotes: 2

Haim Evgi
Haim Evgi

Reputation: 125486

using DATE_SUB

select * from mytable where time <
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 day))

Upvotes: 4

simnom
simnom

Reputation: 2620

Why not just use the PHP mktime function, pass in the date you want and away you go:

http://php.net/manual/en/function.mktime.php

Upvotes: 0

Related Questions