-2

I want to run a query like this

select * from items where expire > 'time()'

so that i can check which item has the validity. I am trying to set future time like this

$expire=time()+3*86400;///3 days later 

But this is not working.Why ?

user2252617
  • 99
  • 1
  • 4
  • 14

1 Answers1

0

using 'time()' in a MySql statement will just insert the string time(), not the actual timestamp.

You should use expire > now() or to use PHP's time function use a statement like:

$sql = "select * from items where expire > '" . time(). "'";

Also, for your PHP statement, it's best to use parenthesis in your equations:

$expire = time() + (3 * 86400);

EDIT:

To show the timestamp / date 3 days from now, your code is correct:

$expire = time() + (3 * 86400);

echo $expire . " - " .  date('d-m-Y H:i:s', $expire);

// OUTPUT: 1420937823 - 11-01-2015 00:57:03 (LONDON TIME)

To use this in a MySql query:

$expire = time() + (3 * 86400);
$sql = "select * from items where expire > $expire";

From OP's latest comment, to do this in AngularJS, either use MomentJS or

var threeDaysFromNow = new Date(new Date().getTime() + 3*24*60*60*1000);
{{threeDaysFromNow.getTime() | date:'medium'}} 
Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • It's showing that becuase its an invalid timestamp. Unix timestamps are the number of seconds since 1st Jan 1970, So its just the default, the same as a timestamp of 0. See [here](http://stackoverflow.com/questions/1090869/why-is-1-1-1970-the-epoch-time) for a better explanation – Scriptable Jan 08 '15 at 00:51
  • How to fix it to show the exact date 11 jan 2015 as am setting 3 days later ? – user2252617 Jan 08 '15 at 00:53
  • Thanks for your answer.But actually i am trying to achieve this with angular js( {{data.expire | date:'medium'}} ) but it always shows 1970 instead of 2015.Any help ? – user2252617 Jan 08 '15 at 05:09
  • Then you are probably looking for moment.js. Not sure why you posted sql queries if you want to do this in JS. You should consider making your question clearer, adding some example code that you have and explain what you've tried already. I've updated the answer – Scriptable Jan 08 '15 at 10:08
  • I am storing time stamp in mysql table then running a php Query to fetch the time stamp then showing data in angular template.Hope it is little bit clear to you now! – user2252617 Jan 08 '15 at 19:17
  • Is there any way to format the time stamp during query ? Tried `SELECT expire , DATE_FORMAT($expire, '%Y-%m-%d %H:%i:%s') AS expire from table` so far but didn't work! – user2252617 Jan 08 '15 at 19:50
  • See this [link](http://stackoverflow.com/questions/9251561/convert-timestamp-to-date-in-mysql-query) – Scriptable Jan 08 '15 at 23:29