So, i am working on an app and i need to figure out if a post someone made is a month old using the MySQL Timestamp that looks like this "2015-12-31 15:17:45".
How would i go about doing this to see if this post is a month old?
|
|
So, i am working on an app and i need to figure out if a post someone made is a month old using the MySQL Timestamp that looks like this "2015-12-31 15:17:45".
How would i go about doing this to see if this post is a month old?
Check out my blog and portfolio at http://calebprenger.com
This would be easier to do in the PHP file that reads the date. Look at the function available:
MySQL :: MySQL 5.7 Reference Manual :: 12.7 Date and Time Functions
This is one way of doing it in JS.
You should check this a lot. I gave it a simple couple of tests, but not extensive.Code:<script type="text/javascript"> var date= new Date(); var year = date.getYear();//alert(year); var month = date.getMonth(); var day = date.getDay(); var str = "2015-12-31 15:17:45"; var Year = str.substr(0,4) - 1900; var toHere = str.indexOf('-') + 1; var small_str = str.substr(toHere); var Month = small_str.substr(0,2); //If the working variable is 1 we are now into next year // The month var starts at 0 so we make it larger then // Month by adding 13. If it's zero we're in the same year wVar = (year - Year); if(wVar == 1) month = month + 13; // process Day just like we did for Month var toHere = small_str.indexOf('-') + 1; var Day = small_str.substr(toHere,2); if (((month - Month) >= 1) && (day > Number(Day))) alert("Over a month"); else alert("Under a month"); </script>
Evolution - The non-random survival of random variants.
Physics is actually atoms trying to understand themselves.
Far easier in SQL before it even gets to the PHP.
Assuming that the field containing the timestamp is called timestampField then the following where clause will only retrieve entries where that timestamp is within the last month (make the > a < instead ot get timestamps older than a month)
WHERE DATE_ADD(timestampField, INTERVAL 1 MONTH) > NOW()
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";which makes it easier to find errors in your code.