Reputation: 2581
Table - user_feed
column name - date_n_time (type - datetime)
How do I write the PHP code to check if the date_n_time
of a particular row in the table - user_feed
is within 5 days from now (for example)?
What's a permanent fix for processing date and time in PHP and SQL?
Upvotes: 0
Views: 676
Reputation: 221
This SQL code would work for PHP as well so to check your condition:
select * from user_feed where date_n_time > GETDATE () -5
Upvotes: 1
Reputation: 161
My favorite way to handle dates and times in PHP with MySQL, is to use the datetime datatype in MySQL then use the MySQL date functions. You can use the date functions to convert the date to a unix timestamp and then modify it in PHP if you like.
Upvotes: 1
Reputation: 3731
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
Upvotes: 1
Reputation: 4749
Assuming you're about to compare it in the scripting side:
abs(strtotime(date_n_time) / (24*3600)) < 5
Please note that this line is both past- and future-proof, because you referred to it as '5 days from now'.
For database side solution, you'll need to check your vendor's documentation.
Upvotes: 1
Reputation: 12648
It could be written in SQL directly and would look something like this (for example):
SELECT INTERVAL 5 DAY + column;
The permanent fix would be reading the documentation:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
https://www.php.net/manual/en/ref.datetime.php
Upvotes: 2
Reputation: 4585
If the value is a linux time-stamp you can compare it to time():
if( date_n_time <= ( time() - 5*24*60*60 ) ){ //days hours mins secs
...
}
Upvotes: 1