Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    174
    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

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,303
    Thanks
    30
    Thanked 864 Times in 862 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.
    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>
    You should check this a lot. I gave it a simple couple of tests, but not extensive.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    8,131
    Thanks
    3
    Thanked 814 Times in 803 Posts
    Quote Originally Posted by sunfighter View Post
    This would be easier to do in the PHP file that reads the date.
    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.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •