Welcome Guest, Not a member yet? Register   Sign In
Comparing Unix Timestamps
#1

[eluser]PHP Creative[/eluser]
Hi there

I was wondering it is possible to compare Unix Timestamps. Basically I have table with date_added column and I would like to only select the records entered this month. This is possible?

Thanks a million
#2

[eluser]danmontgomery[/eluser]
Code:
SELECT * FROM my_table WHERE DATE_FORMAT(FROM_UNIXTIME(date_added), '%c') = '2'

Or, you can create a unix_timestamp in PHP and do a direct comparison... You'll want to watch out for timezone compensation though.

Code:
$start_timestamp = strtotime(date("F 1, Y"));
$end_timestamp = strtotime(date("F t, Y 23:59:59"));
$sql = "SELECT * FROM my_table WHERE date_added >= " . $start_timestamp . " AND date_added <= " . $end_timestamp;

[edit] For past months I guess you would also want to limit the top end of the search
#3

[eluser]PHP Creative[/eluser]
Thanks a million. Thats great!
#4

[eluser]PHP Creative[/eluser]
Sorry for dragging this up again.

How would I go about adding or subtractng days/months/years to the timestamp? I have the tried the following with no luck

Code:
strtotime(date("F -1 Month, Y"));

Thanks a million.
#5

[eluser]danmontgomery[/eluser]
In PHP, strtotime():

Code:
date("F, Y", strtotime("-1 month"));

In MySQL, DATE_SUB() or DATE_ADD()

Code:
SELECT DATE_SUB(NOW(), INTERVAL 1 MONTH);




Theme © iAndrew 2016 - Forum software by © MyBB