Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
Results 1 to 3 of 3
Thread: MYSql timestamp and Javascript
-
01-05-2016, 03:44 PM #1Regular Coder
- Join Date
- Jun 2010
- Posts
- 173
- Thanks
- 0
- Thanked 4 Times in 4 Posts
MYSql timestamp and Javascript
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
-
01-05-2016, 09:24 PM #2Master Coder
- Join Date
- Jan 2011
- Location
- Washington
- Posts
- 6,251
- Thanks
- 30
- Thanked 859 Times in 857 Posts
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.
-
01-05-2016, 10:01 PM #3Master Coder
- Join Date
- Sep 2005
- Location
- Sydney, Australia
- Posts
- 8,055
- Thanks
- 2
- Thanked 808 Times in 797 Posts
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.



Reply With Quote
